Skip to main content
gamedev.net
🔒 Locked

array of an array

Started by VprMatrix89 Jul 21, 2008 at 4:29 AM 1 replies 600+ views
Original Post
VprMatrix89
VprMatrix89
So I needed an array of pointers, as this is the only way I could think of allocating memory. Then, I needed to allocate array on the new store using one of those pointers, and finally, get the address of that array. int * array[20] array[0] = new int [500]; //appears legal int **address = &array[0]; **(address + x ) = 1; so what I get is a pointer to a pointer? Is this correct for assignation?
Evil Steve
Evil Steve
Quote:
Original post by VprMatrix89
So I needed an array of pointers, as this is the only way I could think of allocating memory. Then, I needed to allocate array on the new store using one of those pointers, and finally, get the address of that array.

int * array[20]
array[0] = new int [500]; //appears legal
int **address = &array[0];
**(address + x ) = 1;


so what I get is a pointer to a pointer?
Is this correct for assignation?
Yup, that's correct. That last line is the same as:
address[x][0] = 1;
Note that your code only allocates the 2nd dimension of the first element of the array. If you wanted to allocate an 2D array that's 20x500 elements, you'd want to use a for loop:
int* array[20];for(int i=0; i<20; ++i)   array = new int [500];
Zahlman
Zahlman
Just use boost::multi_array and be done with it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.