Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of int (*pt)[5] in c [duplicate]

Tags:

arrays

c

pointers

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.

like image 511
Janakiram Reddy Avatar asked Dec 14 '25 07:12

Janakiram Reddy


2 Answers

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]

like image 196
Gopi Avatar answered Dec 16 '25 23:12

Gopi


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, therefore
  • arr is an int (*)[5], a pointer to an array of five integers.
like image 23
Jon Purdy Avatar answered Dec 16 '25 23:12

Jon Purdy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!