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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int **twoDarray; // 2-D dynamic array of integers, pointer declaration
int xdim = 25;
int ydim = 30;
twoDarray = newint* [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] = newint [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.
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;