I Have read some where about this type declaration. Declaration is:
int (*arr)[5];
i'm little bit confused with this type declaration.Actually what is the meaning of this declaration and when do we use it.
I Have read some where about this type declaration. Declaration is:
int (*arr)[5];
i'm little bit confused with this type declaration.Actually what is the meaning of this declaration and when do we use it.
int *arr[5]
arr
is array of 5 pointers
int (*arr)[5]
arr
is a pointer to an array of 5 integer elements
Check the code below:
int a[5] = { 1,2,3,4,5};
int (*arr)[5] = &a;
printf("%d",(*arr)[2]);
Now the array element can be accessed like
(*arr)[i]
not *arr[i]
It means arr
is a pointer to an array of 5 integers. Compare with the less-confusing array of five pointers:
int* arr[5];
That's why you need the parentheses.
According to the “declaration follows use” rule:
(*arr)[i]
is an int
, where i <= 5
, therefore*arr
is an int[5]
, an array of five integers, thereforearr
is an int (*)[5]
, a pointer to an array of five integers.