Quote:
Originally Posted by Jessica Ok, two short questions. This is not homework, if you are wondering.
The first question:
consider this line:
int multi[30][80];
Which of the following is 'multi'
- An array of 30 arrays, each containing 80 ints
- An array of 80 arrays, each containing 30 ints
Second part of the question:
If I did these three lines:
int a = multi[5][15];
int** ptr = &multi[0][0];
int b = ptr[5][15];
is (a == b) for all cases? |
Because, in C++, multiple array indices increment right-to-left (in other words, multi[0][0] is next to multi[0][1] in memory and multi[1][0] is eighty positions past multi[0][0]), the second index indicates the column and the right, the row. This makes it 30 rows of 80 columns. (The 80, being the nominal size of an input line (holdover from the ancient punch cards), is also a subtle tip-off to an old geezer like me.)
Pointers-to-pointer (**) are usually used in two situations: 1. A pointer argument to a function that's expected to return a new value OF THE POINTER or 2. A pointer to an array of pointers. This is neither. In fact, int* ptr = &multi[0][0]; is the correct syntax. (Which is identical to int* ptr = multi; by the way.) So if the compiler permits subscript notation with a pointer (I don't remember if that's syntactically legal), yes, a would always have the same value as b.
Hope that helps.