I'm learning pointers to (entire) arrays in C.
Suppose I declare a 2d matrix of ints:
int arr[3][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Now, I declare a pointer of appropriate type:
int (*ptr)[3][3];
Initialize it:
ptr = &arr;
Now, ptr contains the address of arr.
ptr --->&(arr[0][0] arr[0][1] arr[0][2]
arr[1][0] arr[1][1] arr[1][2]
arr[2][0] arr[2][1] arr[2][2])
So, when we dereference ptr, it should yield the first location, isn't it?
printf("%d", *ptr) gives an error at compile time. To print the first element, I've to use ***ptr.
I have 2 questions:
- I can't understand the need for a triple dereferencing.
- If
*ptrdoesn't point to first element of array, what does it point to?
*&arrto refer toarr[0][0]instead ofarr? That's like expecting*&some_intto refer to the int's first byte, rather than the entire int.