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?
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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With