Forgive me for my poor English, me and my friend were doing school homework, suddenly he asked me to read this line of code ptr = &array[1][1][1]
(ptr is a pointer to an integer).
He said
ptr is pointing to the address of the second element of the second
column of the second row of an array.
I think he will be right if it was an array of char. As far as it’s an integer 3D array I have no idea how to read that line of code in plain English.
How would you read it?
2
If you have 3 indexes, then it must be a three dimensional array. Since C only supports jagged arrays, a multidimensional array is simulated by a three-level jagged array. In other programming languages (like C#) a multidimensional array is one object having several dimensions. In order to access one element of the array you need as many indexes as there are dimensions.
A jagged array is effectively an array of arrays of arrays. It is a construction made up of many array objects linked together in a tree like manner. Since the top level array of a dynamically allocated array can contain references to other arrays of different lengths, it is called jagged array or ragged array. A statically allocated array is always rectangular. Example: Access an element in a 3-level jagged array:
int x = a[2][0][4];
Now, your expression is preceded by an &
which means “address of” when used as unary operator. (Don’t confuse with the binary &
operator which is the binary AND operator.)
The expression a[2][0][4]
points to (or is the address of) an element of the array of the array of the array. Therefore &a[2][0][4]
is a pointer to this address. It is common to think of two dimensional arrays as of rows and columns. Now each element in this row/column is again an array. Therefore you have a pointer to the address of an element of a column of a row of an array. In my example this is the fifth element of the first column of the third row. Arrays indexes are zero-based, therefore a[0]
points to the first element.
“Pointer” is a high level term used in C, where as “address” is a low level term referring to the implementation. But both denote references to memory locations.
8
Let’s assume we have a 2d-array like (for simplicity sake, the process for higher dimensionality is the same)
int array[3][3];
The array viewed in memory is one dimensional, but logically we can think of it like this
array
----------------------------
| [0][0] | [0][1] | [0][2] |
----------------------------
| [1][0] | [1][1] | [1][2] |
----------------------------
| [2][0] | [2][1] | [2][2] |
----------------------------
When you call array[1][1]
, you are retrieving the value at that location (which I chose below to be 12)
----------------------------
| 0 | 0 | 0 |
----------------------------
| 0 | 12 | 0 |
----------------------------
| 0 | 0 | 0 |
----------------------------
This would allow us to construct code like the following
int value = array[1][1];
std::cout << value << std::endl;
Output
12
When you call &array[1][1]
, you are retrieving the pointer to that location
The following code would retrieve the pointer to the “center” of the array
int* ptr = &array[1][1];
(*ptr) = 10;
Which would change the value in that location to 10
. This would result in the array to look like
----------------------------
| 0 | 0 | 0 |
----------------------------
| 0 | 10 | 0 |
----------------------------
| 0 | 0 | 0 |
----------------------------
Basically, the process for interpreting ptr = &array[1][1][1]
is the same, but you are dealing with an array of higher dimensionality.
It is the pointer to the second element of the array in each dimension. Your teacher would’ve been correct if your code said ptr = &array[0][0][0]
If you have a threedimensional array, then rows and colums doesn’t apply, so I would say something like:
The variable ptr
is assigned the address of the second item in the third dimension of the second item in the second dimension of the second item in the first dimension of the array.
4