-1

What is the difference between int *a[10] and int (*a)[10]?

I suspect that since [] operator has higher precedence than * operator, the first case will be an array consisting on 10 pointer variables storing the address of integer.
The 2nd case is the one which I am unsure what it does and why?

haccks
  • 104,019
  • 25
  • 176
  • 264
metasj
  • 65
  • 1
  • 7

2 Answers2

2

int *a[10] declare a as an array of 10 pointers to int.
int (*a)[10] declare a as a pointer to an array of 10 ints.

Remember these two rules to decipher pointer declarations:

  • Always read declarations from inside out: Start from innermost, if any, parenthesis. Locate the identifier that's being declared, and start deciphering the declaration from there.

  • When there is a choice, always favour [] and () over *: If * precedes the identifier and [] follows it, the identifier represents an array, not a pointer. Likewise, if * precedes the identifier and () follows it, the identifier represents a function, not a pointer. (Parentheses can always be used to override the normal priority of [] and () over *).

For example:

int *a[10];      "a is"  
     ^  

int *a[10];      "a is an array of 10"  
      ^^^^ 

int *a[10];      "a is an array of 10 pointers"
    ^

int *a[10];      "a is an array of 10 pointers to `int`".  
^^^      
haccks
  • 104,019
  • 25
  • 176
  • 264
1
  • int *a [10] - is declaring an array named a of 10 int *s.

  • int (*a)[10] - is declaring a pointer named a to an array of 10 ints.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261