5

Lets say we have : int A [5] [2] [3]; Now, if I do : A[1][0][0] = 4; does that mean :

1.) A [1] and A [1][0] are pointers ?

2.) If A[1] is a pointer, then it will store the address of the pointer A[1][0] ?

3.) If A[1][0] is a pointer, then it will store the address of A[1][0][0] which is NOT a pointer but just a variable that stores the value of 4 ?

If the above points are correct, then why dos the following code give us the same integer address :

int main(void)
{
        int A [5] [2] [3];
    A[1][0][0]=4;

    printf("%d\n\n", A[1]);
    printf("%d\n\n", A[1][0]);
    printf("%d\n\n",&A[1][0][0]);

        system("pause");
}

Here, I am assuming that A[1] is a pointer which points to another pointer A[1][0] and hence stores the address of the pointer A[1][0]. And, A[1][0] is a pointer which points to the VARIABLE A[1][0][0] and hence stores the address of the VARIABLE A[1][0][0] .

Please help me!

John Nash
  • 67
  • 5
  • Not directly related to your question but 'printf()' format "%d" expects an 'int' and if you don't provide one you hit Undefined Behaviour, which is Bad. You give it 'A[1]' an array-of-2x3-int decaying to pointer-to-int, 'A[1][0]' an array-of-3-int decaying to pointer-to-int, and '&A[1][0][0]' a pointer-to-int. If sizeof(int) < sizeof(int*) you'll likely see just part of the pointer value; if sizeof(int) > sizeof(int*) you'll likely see garbage; but a suitably Machiavellian compiler could emit code to erase your hard disk and still conform to the Standard. – mlp May 25 '12 at 03:04

6 Answers6

4

To properly answer your question, read up on row-major order, which is how multidimensional arrays are stored in C. The Wikipedia article is a little too terse, but one of these might be more clear:

http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html http://archive.gamedev.net/archive/reference/articles/article1697.html http://www.ibiblio.org/pub/languages/fortran/append-c.html

There's this SO question also.


In direct answer to your points, assuming that you know how row-major storage works:

int A[5][2][3] declares a contiguous region of memory that is 5*2*3 ints long: five arrays of two arrays of three ints each. The arrays are stored next to each other in linear memory, so that

&A[0][0][0] == A
&A[0][0][1] == A+1
&A[0][1][0] == A+(1*3)
&A[3][1][2] == A+(3*(2*3))+(1*3)+2

A[1] is technically not a pointer but an array. It's an int [2][3] array. But I find that much less clear to think about than considering A[5][2][3] is a flat region of memory, thirty ints long.

A[0][0][0] is the first integer in that region. 
A[0][0][1] is the second integer. 
A[0][0][2] is the third integer. 
A[0][1][0] is the fourth integer in this flat region. 
A[0][1][1] is the fifth integer. 
And so on until A[1][0][0] is the eleventh integer. 

Therefore the address of A[1][0][0] is ten integers past A[0][0][0]; ie, &A[1][0][0] - &A[0][0][0] == 10. Because the C language is very loose about the difference between arrays and pointers, A[1] gets interpreted as if it were an address when you use it in an expression, even though it really means "the first element in an array of five arrays of two arrays of three integers" which is in turn "an array of two arrays of three integers."

The upshot is that A[1] doesn't store a pointer, it is a pointer. Every memory address from &A[0][0][0] to &A[5][2][3]-1 stores an integer in your multidimensional array.

What you're thinking of in points (2) and (3) are arrays of pointers to arrays, which are something different.

This is a lot easier to explain with pictures, which is why you should find a proper textbook or article on C arrays.

In general, when learning about pointers and arrays in C, I recommend that you temporarily forget about the language itself and pretend that you are Dennis Ritchie inventing C on a PDP-11 computer with 56kb of flat RAM. Get a big sheet of graph paper, number its cells consecutively, pretend that it represents your RAM and each cell is one byte, and you can work through your pointer math with pencil and paper.

C was invented in that environment and understanding its origins will make the modern langauge much more sensible.

As a side note, when I tried to write this answer, Stack Overflow's markup language repeatedly changed and screwed up the indexes in my array examples above. So if you see any numbers there that seem out of range for their arrays, it's a mistake introduced by the editor.

Community
  • 1
  • 1
Crashworks
  • 38,324
  • 12
  • 96
  • 168
1

Multidimentional arrays in c do not make use of pointers. Although a pointer-to-pointer-to-pointer access might look similar, The actual data may not be contiguous, and there is overhead to store all the addresses. Access of multidimentional arrays in C is a form of syntactic sugar:

char a[5][7][9]
a[d][h][w]  <<==>>  ((char*)a)[((9*7*d)+(9*h)+w] 

C arrays all share the property that they decay(or automatically turn into pointers to the first element in the array). As a result,

a[1]  (char[7][9])    --decay-->   ((*char)[5][9]) pointer to char array
&a[1] ((*char)[5][9])  no decay

The two are equivalent, because in the later you explicitly "decay" the pointer, whereas it happens automatically in the first.

Dave
  • 10,479
  • 3
  • 27
  • 52
  • But how can we have the same output for this: int main(void) { int A [5] [2] [3]; A[1][0][0]=4; printf("%d\n\n", A[1]); printf("%d\n\n", &A[1]); system("pause"); } – John Nash May 24 '12 at 23:43
  • I couldnt format it. But the question is : Will A[1] and &A[1] give the same output ? – John Nash May 24 '12 at 23:44
  • They turn into pointers to their first element when the situation demands a pointer. – Dave May 25 '12 at 00:28
  • Can you explain me how a[1] (char[7][9]) would decay to ((*char)[5][9]) ? – John Nash May 25 '12 at 00:41
1

Your assumptions would be true if you had dynamic arrays (i.e. allocated with malloc/calloc).

Static arrays, however, are allocated as contiguous chunk of memory and are simply pointer to the first element. When you write A[X][Y][Z] it is basically equivalent to *(A + X*YSIZE*ZSIZE + Y*SIZE + Z), not to *(*(*(A+X) + Y) + Z). This allows for somewhat faster access to the data (you do not need to access intermediate pointers), but requires all the data to be allocated in one chunk and be of regular sizes.

Here is more info regarding non-interchangeability of static vs. dynamic arrays in C.

aland
  • 4,254
  • 2
  • 20
  • 38
1

The variable A is 5 * 2 * 3 ints, allocated together as a block. (I.e., 30 ints).

There are no pointers involved in the declaration `int A[5][2][3];' - the only space set aside is to hold the 30 int values.

When you write an expression using A and subscripts, because you have said there are 3 dimensions, you must provide all 3 in order to specify which int value you are accessing or altering. If you use fewer than 3 subscripts, you are only partially specifying what you are accessing; the convention is that such a reference is taken as a request for the address of the relevant part of the overall space.

Art Swri
  • 2,549
  • 3
  • 22
  • 36
  • But why will A[1] and &A[1] give us the same output : it gives the same integer address. – John Nash May 24 '12 at 23:46
  • @John Nash: 'A[1]' is a 2x3 array of 'int', and in a pointer-context it decays to the address of that array. '&A[1]' is "the address of 'A[1]'". – mlp May 25 '12 at 02:49
  • It's just a convention adopted by the creators of the C language. It seemed to them like the most reasonable interpretation of the expression and reasonably 'natural'. – Art Swri May 25 '12 at 03:40
0

Read up on Row Major Order

Yeah, that didn't help at all, I was thinking C was column major for a second there I think. (forgetting what all the stuff meant)

Mark Harviston
  • 662
  • 4
  • 18
0

It might be a little weird, but here's my explanation. Imagine you have six distinct people numbered from 1 to 6, and they are lined up accordingly from 1 to 6. If you told that this team (the six people) is divided into two groups, where the first three (1-3) are in group A, and the rest (4-6) are in group B.

[1 2 3] [4 5 6]

Then, if I tell you who is the first one in the TEAM? You would say the first one! But if I ask you who is the first member in GROUP A of THE TEAM? It is the same, the first person!

Also, if I tell you who is the fourth member in the team? You would say the fourth! Then, what if I ask you who is the first member in GROUP B of THE TEAM? It is the same, the fourth one.


The same story happens to you; A[1] is a pointer to beginning of the big array (The Team), where A[1][0] tells to point to the the beginning of the first inner array (THE FIRST GROUP) inside the big array (A[1]), which is identical! Then you say, &A[1][0][0], which is like going to ask the first member of the first inner array which resides in the big array, what is your number? Then, he will replay the same.

What differs is the type of the pointers and how they are interpreted, but their values are identical. This because arrays stores elements in contiguous way.

Ghasan غسان
  • 4,866
  • 4
  • 27
  • 39