Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   multi dimensional array error (http://www.programmingforums.org/showthread.php?t=11901)

veiga2 Nov 16th, 2006 3:09 AM

multi dimensional array error
 
i hav a problem bout arrays... why cant i allocate the variable in this array?
thnx for your help

#include<iostream>
using namespace std;


main()
{
int r=1,c=1,r2,c2;

cout<<"Enter the no. of rows you desire for matrix1:";
cin>>r;
cout<<"Enter the no. of coloumns you desire for matrix1:";
cin>>c;
cout<<"Enter the no. of rows you desire for matrix2:";
cin>>r2;
cout<<"Enter the no. of coloumns you desire for matrix2:";
cin>>c2;

if(r2>c)
cerr<<"invalid row input";


int **matri1[]=new int[r][c];// source of error<----- this is it





for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
cin>>matri1[i][j];
}

for(int k=0;k<r;k++)
{
for(int j=0;j<c;j++)
{
cout<<matri1[k][j];
}
cout<<endl;
}







}

bl00dninja Nov 16th, 2006 3:52 AM

int **matri1[]=new int[r][c];// source of error<----- this is it

new returns a pointer to whatever you just made.

you just made a 2-d array of ints.

you're trying to assign a 2-d array of ints to a pointer to a pointer to a single array of ints or some other weird crap. your types don't jive...

apple = 3;

purple = 3.14159;

this shit doesn't work...the type of the thing on the left has to be the same as the type of the thing on the right.

actually i'm not even sure what you did, but this is weird!

if you have X = Y;

then X and Y have to be the same kind of thing.

Jimbo Nov 16th, 2006 4:05 AM

First off, you're making a pointer to a pointer to an array of integers. From a simplistic view (though not entirely accurate, grumpy or Narue can and have explained much better if you want the nitty-gritty) this could be simplified as either int*** or int[][][]. I'm guessing that's not what you want.

For the sake of I-should-be-in-bed-right-now, I'll just give a quick sample which should work... but if it doesn't, well, sorry
:

int **matri1 = new int*[r];
for(int i = 0; i < r; i++)
  matri1[i] = new int[c];

Note that I dropped the [] from the declaration of matri1. Then I initialized it as an array of int*. Then I made each of the int* into an array of size c.


All times are GMT -5. The time now is 1:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC