Why can't I dereference a pointer to a multidimensional array?
The main question .. had to ask. Any help would be appreciated.
Q: Why can't I dereference a multidimensional array pointer like this:
int arr [2][2] = { {1, 2} , {3, 4} };
printf("%d ", *arr);
You can dereference it, it just doesn't produce what you expect: *arr
not int
, it's a pointer to int
(OK, one-dimensional array). If you want to print 1
, add another star:
printf("%d ", **arr);
Try:
int arr [2][2] = { {1, 2} , {3, 4} };
printf("%d ", **arr);
You need two levels of dereferencing since your array is two dimensional.
If a
- int[][]
, then *a
- int[]
. You need a different level of redirection to access the array element. That is **a
- int
.
Remember, if we define a as int [] [], then it is a two-dimensional array, and it can be dereferenced ** a. If the array is one-dimensional, we have to use * a to dereference it ...
Try it.