3

The declarations

int arr[10];
int *arr1[10];

are straightforward. The first declares an array of 10 integers while the second declares a 10 value array of pointers to int (int *).

Now, consider these declarations:

int (*arr2)[10];
int *(*arr3)[10];

What do these even mean? Considering the parenthesis, they are trying to dereference arr2 and arr3 in the declaration? Is this even possible?

Can anyone answer what is the type of values stored in these arrays and if I do something like *arr2 or *arr3, what would the value be?

Also, what is the type of arr2 and arr3 now?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Black Wind
  • 313
  • 1
  • 8

2 Answers2

3

In these declarations:

int arr[10];
int *arr1[10];
int (*arr2)[10];
int *(*arr3)[10];

arr is an array[10] of ints.

arr1 is an array[10] of pointers to int.

arr2 is a pointer to an array[10] of ints.

arr3 is a pointer to an array[10] of pointers to int.

Harith
  • 4,663
  • 1
  • 5
  • 20
0

When reading declarations in C where the name is enclosed in parentheses, what helps me is to start at there (hopefully when there is only one name inside it) and, when there is nothing to the right, go left, until you reach a parenthesis, then you go right from the whole parentheses until you reach a parenthesis or the end etc.

So int (*arr2)[10] would be (left) a pointer (*) to (right) an array ([10]) of (left) int. int *(*arr3)[10]; would be a pointer (*) to an array ([10]) of (left) pointer (*) to int.

IS4
  • 11,945
  • 2
  • 47
  • 86
  • Mmh, it's the "right-left" rule, not "left-right". In your example you start from arr2, you try to move right, but there's a parenthesis, so you move left and you find the star. So arr2 is a pointer to something. Then, the content of the parentheses is over, so you go again to the right, and continue like that. Otherwise the rule won't work for `int *arr1[10];` – Fabio says Reinstate Monica Mar 25 '23 at 17:37
  • Yeah, that works better with generalized names that are not enclosed in parentheses – IS4 Mar 25 '23 at 17:39