1

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
  • 2
    [1] is the second element not the first element of an array. Arrays in C start at zero.
    – Reactgular
    Dec 31, 2013 at 15:10
  • my bad, thanks @MathewFoscarini for pointing it out, i'm going to edit my mistakes. :)
    – Hanoch
    Dec 31, 2013 at 15:34

3 Answers 3

4

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
  • 3
    "C makes a difference between multidimensional and jagged arrays." Could you clarify this sentence? Because I've seen this distinction in languages such as C#, but never in C. All sources I've seen suggest that C only has arrays of arrays (jagged arrays).
    – luiscubal
    Dec 31, 2013 at 16:37
  • Please cite a reference for the a[2, 0, 4] syntax. AFAIK that's not C.
    – Caleb
    Dec 31, 2013 at 16:52
  • A jagged array is an array of arrays (which of course may contain arrays) so you could have a[2][1][1] but a[1][1][1] cannot be guaranteed to exist (as a[1][1] may have been assigned only a single element array.) In a multidimensional version of the example each item of the 3 indices are initialised and therefore will exist. Dec 31, 2013 at 16:52
  • 1
    You are right of course. I corrected my answer. Dec 31, 2013 at 17:03
  • 2
    @JamesSnell I'm not sure it's really correct to even call C's arrays "jagged" since the sub-parts are all the same size and implemented as a contiguous block of memory. A multidimensional array in C is not an array of pointers to arrays, just as a string is not an array of pointers to characters.
    – Caleb
    Dec 31, 2013 at 17:10
6

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]

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
  • 2
    "rows" and "columns" are just terms that help us visualize the data layout; there's no reason they can't apply to two of the three dimensions. You could a term such as layer, page, or sheet for the third dimension.
    – Caleb
    Dec 31, 2013 at 16:56
  • @caleb: You could call the dimensions whatever you like, but there is no natural choise to add a third dimension after rows and columns that would make it obvious that its a third dimension. You could call it elephants if you like, but it won't help anybody.
    – Guffa
    Dec 31, 2013 at 19:18
  • The point is that there's no reason to abandon the convention of calling two of the dimensions "rows" and "columns", respectively, just because you lack an equally conventional name for the third dimension. Your claim that the convention "doesn't apply" seems unjustified. Indeed, simply using another term along with the conventional ones conveys the idea rather well: the second column of the third row of the fifth elephant (assuming elephant-major order).
    – Caleb
    Dec 31, 2013 at 19:48
  • @caleb: You are missing the point. The row and column analogy only works for two dimensions. When you have any other number of dimensions it's no longer obvious what dimension a row or column would be, or what any other dimension would represent.
    – Guffa
    Dec 31, 2013 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.