Quick question about using arrays in data structures

Hi guys,

I've got a quick question about this block of code in my textbook. If someone could clarify exactly what it does I'd be really helpful.

int **twoDarray; // 2-D dynamic array of integers, pointer declaration

int xdim = 25; 
int ydim = 30; 

twoDarray = new int* [xdim]; // so this is now creating an array of pointers in heap memory the 
                                            //size of xdim which is 25 that's pointed to by **twoDarray

for (int i = 0; i < xdim; i++)

twoDarray [i] = new int [ydim]; // this part I'm a bit confused on... the book says this: 
//"For each pointer in the pointer array, a dynamic array is created for it to point to." However, I'm not 
//really sure what is it doing with twoDarray[i]. Is it referring to twoDarray = new int* [xdim] and saying 
//for each value of the array we are going to have it point to new int [ydim]? 

for (i = 0; i < xdim; i++) // loop to go through all everything in the xdim array
  for (j = 0; j < ydim; j++) // look to go through ydim array
    twoDarray[i][j] = i*j; // I'm not exactly sure what this line does. I know it's assigning i*j to twoDarray[25][30]
// but what exactly does i*j mean? Is it i x j? And if so it's assigning the product to the contents of both array [i] and [j]? 

Hope someone could clarify these questions for me. I know they might be a little silly but I really want to understand every little thing before I move on.

Thank you!
Last edited on
Line 9: You want a 2D array. Line 6 creates the first dimension. Line 9/11 create a dimension for each first first dimension entry.

Line 18: Apply a number (the product of the indexes) to each value in your 2D array.
Thanks for the clarification. I've got 1 more question though. On line 18 where exactly does it apply the product of the indexes to? Does it apply the product of i*j to both [i] and [j] since it is written as twoDarray[i][j] = i*j;
i si the index to dimension 1.
j si the index to dimension 2.
i * j is the product of the indexes

I'm not sure where you're getting lost.
Topic archived. No new replies allowed.