August 16th, 2009, 09:06 PM
#1
pointers for dynamic 2D arrays
hi. i am wanting to know how to make a 2d dynamic array.
if i typically compiled :
int row = 2;
int column = 3;
int a[row][column];
That would cause an error in comilation saying non constant array bounds
But, for the 1D case,I can overocme this by :
int * ptr = NULL;
int row;
cin >> row;
ptr = new int[row];
That will create a dynamic array where ptr points to the first element/
But when I try this for the 2D case, it doesnt work. THe prototype for the 2D case would be:
int row;
int column;
int * ptr = NULL;
cin >> row;
cin >> column;
ptr = new int[row][column];
This would be the prototype ; i.e. for other datatypoes i just switch int with what i want.
In my case, I have a custom datatype:
class Pixel
{
char r;
char g;
char b;;
}
I then got rows and columns into variable unsigned integer rows, and unsigned integer columns
Then I created:
Pixel * ptr = NULL;
This works: ptr = new Pixel[rows] ;
But this does not work: ptr = new Pixel[rows][columns]; visual c++ gives :
error C2540: non-constant expression as array bound
and
error C2440: 'initializing' : cannot convert from 'Pixel (*)[1]' to 'Pixel *'
HEPL PLEASE
August 16th, 2009, 09:07 PM
#2
Re: pointers for dynamic 2D arrays
just realized that i should have used the search function to see if this topic qwas already done before. sorry if it has been just realized this havent poste din a long time./
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks