Multi arrays
-
What I want to do is take an input from the user to set up a grid. This grid is a multi- dimensional array. I can do this with a simple array (1-D) and create the object as new. But when I try this with a multi- dimensional I get the error "constant expression expected." If only I had more time!
-
What I want to do is take an input from the user to set up a grid. This grid is a multi- dimensional array. I can do this with a simple array (1-D) and create the object as new. But when I try this with a multi- dimensional I get the error "constant expression expected." If only I had more time!
bitsNbites wrote: But when I try this with a multi- dimensional I get the error "constant expression expected." Without showing the code in error, it's impossible to offer any suggestion(s).
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
bitsNbites wrote: But when I try this with a multi- dimensional I get the error "constant expression expected." Without showing the code in error, it's impossible to offer any suggestion(s).
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!
-
The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!
-
The code that has the error is not correct. I think I need to try a different method, but this is it. In the header I have an int pointer. int* sizeArray; Then in a function in the class. int row=22, col=20; sizeArray = new int[40]; // This is OK sizeArray = new int[row]; // This is OK sizeArray = new int[row][col];// This errors. "non-constant expression as array bound" and '=' : cannot convert from 'int (*)[1]' to 'int *' row and col will be values taken as arguments but for testing I just assign a value. If only I had more time!
int _tmain(int argc, _TCHAR* argv[]) { int** sizeArray; int row=22, col=20; sizeArray = new int*[row]; for(int i=0; i<row; i++) sizeArray[i] = new int[col]; for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { sizeArray[i][j] = i*j; } for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { printf("sizeArray[%d][%d] = %d\n",i, j, sizeArray[i][j]); } return 0; }
http://www.priyank.in/ -
int _tmain(int argc, _TCHAR* argv[]) { int** sizeArray; int row=22, col=20; sizeArray = new int*[row]; for(int i=0; i<row; i++) sizeArray[i] = new int[col]; for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { sizeArray[i][j] = i*j; } for(int i = 0; i<row; i++) for (int j=0; j<col; j++) { printf("sizeArray[%d][%d] = %d\n",i, j, sizeArray[i][j]); } return 0; }
http://www.priyank.in/Great that helps a lot. I now am using: int** q = new int*[rows]; for(int i = 0; i < rows; i++) q[i] = new int [col]; This works. MSDN states "Multidimensional arrays are not equivalent to arrays of pointers." It seems that here this code is creating just that. I suspect this is why I don't see the elements in the Autos window. I will play around with this for a while and will post new questions later. Thanks for the help. If only I had more time!
-
Great that helps a lot. I now am using: int** q = new int*[rows]; for(int i = 0; i < rows; i++) q[i] = new int [col]; This works. MSDN states "Multidimensional arrays are not equivalent to arrays of pointers." It seems that here this code is creating just that. I suspect this is why I don't see the elements in the Autos window. I will play around with this for a while and will post new questions later. Thanks for the help. If only I had more time!
You can always create multi dimensional arrays by arrays of pointers pointing to another array. The difference I think is basically in memory locations, creating arrays by this method dynamically may not have continous memory locations, like that by creating arrays like
int a[5][5];
http://www.priyank.in/