2

I was at an interview today, and was asked the difference between the following two declarations:

int *A[10];

and

int (*A)[10];

which I did not know. If you think I am missing some important pointer 'pointer', please let me know that too. Thanks.

KK.
  • 783
  • 8
  • 20

2 Answers2

8

The first declares an array of ten pointers to int, the second a pointer to an array of ten ints.

The [] binds tighter than the *, so the first could equivalently be written

int *(A[10]);
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

Given

int *A[10];  // an array of 10 int pointers

the relative precedences of the unary * and []makes this equivalent to

int *(A[10]);  // same

The second declaration changes that implicit ordering to

int (*A)[10]; // a pointer to an array of ten ints

C and C++ Operator Precedence and Associativity

Levon
  • 138,105
  • 33
  • 200
  • 191