Iceman
Iceman

Reputation: 4362

Why can't I dereference a pointer to multidimensional array?

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

Answers (4)

Muhammad Zia
Muhammad Zia

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

Sergey Kalinichenko
Sergey Kalinichenko

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

Jonathan Wood
Jonathan Wood

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

icyrock.com
icyrock.com

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

Related Questions