Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer address in a C multidimensional array

I'm messing around with multidimensional arrays and pointers. I've been looking at a program that prints out the contents of, and addresses of, a simple array. Here's my array declaration:

int zippo[4][2] = { {2,4},             {6,8},             {1,3},             {5,7}   }; 

My current understanding is that zippo is a pointer, and it can hold the address of a couple of other pointers. By default, zippo holds the address of pointer zippo[0], and it can also hold the addresses of pointers zippo[1], zippo[2], and zippo[3].

Now, take the following statement:

printf("zippo[0] = %p\n", zippo[0]); printf("  *zippo = %p\n", *zippo); printf("   zippo = %p\n", zippo); 

On my machine, that gives the following output:

zippo[0] = 0x7fff170e2230   *zippo = 0x7fff170e2230    zippo = 0x7fff170e2230 

I perfectly understand why zippo[0] and *zippo have the same value. They're both pointers, and they both store the address (by default) of the integer 2, or zippo[0][0]. But what is up with zippo also sharing the same memory address? Shouldn't zippo be storing the address of the pointer zippo[0]? Whaaaat?

like image 450
Ichimonji10 Avatar asked Jan 05 '10 02:01

Ichimonji10


People also ask

How do you implement a pointer in a multidimensional array?

Pointers and two dimensional Arrays: In a two dimensional array, we can access each element by using two subscripts, where first subscript represents the row number and second subscript represents the column number. The elements of 2-D array can be accessed with the help of pointer notation also.

Can we store base address of 2D array in pointer?

Assigning 2-D Array to a Pointer Variable Hence to store the base address of arr , you will need a pointer to an array of 3 integers. Similarly, If a 2-D array has 3 rows and 4 cols i.e int arr[3][4] , then you will need a pointer to an array of 4 integers. int (*p)[3];


1 Answers

When an array expression appears in most contexts, its type is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to point to the first element in the array. The exceptions to this rule are when the array expression is an operand of either the sizeof or address-of (&) operators, or when the array is a string literal being used as an initializer in a declaration.

Thus, the expression zippo "decays" from type int [4][2] (4-element array of 2-element arrays of int) to int (*)[2] (pointer to 2-element array of int). Similarly, the type of zippo[0] is int [2], which is implicitly converted to int *.

Given the declaration int zippo[4][2], the following table shows the types of various array expressions involving zippo and any implicit conversions:

 Expression    Type            Implicitly converted to  Equivalent expression ----------    ----            -----------------------  --------------------- zippo         int [4][2]      int (*)[2]                &zippo        int (*)[4][2]        *zippo        int [2]         int *                    zippo[0] zippo[i]      int [2]         int * &zippo[i]     int (*)[2]                                *zippo[i]     int                                      zippo[i][0] zippo[i][j]   int &zippo[i][j]  int * *zippo[i][j]  invalid 

Note that zippo, &zippo, *zippo, zippo[0], &zippo[0], and &zippo[0][0] all have the same value; they all point to the base of the array (the address of the array is the same as the address of the first element of the array). The types of the various expressions all differ, though.

like image 138
John Bode Avatar answered Sep 20 '22 19:09

John Bode