Jump to content
  • Advertisement
Sign in to follow this  
VprMatrix89

array of an array

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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];

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!