Reputation: 4362
Basic question.. had to ask. Any help will be appreciated.
Q: Why can't I dereference the pointer to a multidimensional array like this:
int arr [2][2] = { {1, 2} , {3, 4} };
printf("%d ", *arr);
Upvotes: 4
Views: 5530
Reputation: 11
Remember if we define a as int[][], then it means it is a two dimensional array and it can be dereferenced by **a. If array is one dimensional then we should use *a to dereference it...
Try it..
Upvotes: 0
Reputation: 726579
You can dereference it, it is just that the result is not going to be what you'd expect: *arr
is not an int
, it's a pointer to an int
(OK, a one-dimensional array). If you want to see 1
printed, add another star:
printf("%d ", **arr);
Upvotes: 5
Reputation: 67193
If a
is int[][]
then *a
is int[]
. You need another level of redirection to access an array element. That is, **a
is int
.
Upvotes: 2
Reputation: 28598
Try:
int arr [2][2] = { {1, 2} , {3, 4} };
printf("%d ", **arr);
You need two levels of dereferencing, as your array is two-dimensional.
Upvotes: 1