Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers , Multidimensional arrays and Addresses

Tags:

c

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!

like image 513
John Nash Avatar asked May 24 '12 23:05

John Nash


1 Answers

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.

like image 191
Crashworks Avatar answered Sep 23 '22 12:09

Crashworks