Why do we use parentheses in a pointer declaration?

8

What's the difference between these two statements?

int* array1[10];
int (*array2)[10];

Why are there parentheses in the second?

    
asked by anonymous 10.01.2017 / 11:36

1 answer

9

We can see that both are arrays .

int* array1[10];

This is a case where array will have data elements such as "pointers to int ". Then what will be stored in it will be memory addresses. The value of each element is elsewhere pointed to by the data stored in the array .

int (*array2)[10];

Here the pointer is part of the array and not the array element type. So we have a pointer to the array (which is still a pointer , but this is another subject), so the variable array2 will be a memory address that will indicate where the array is. The elements of this array will be of type int , so the value is already inside the array .

C has a rather intuitive way of declaring variables with compound types. C ++ inherited this. In this case the parentheses are language constructs to disambiguate the syntax and indicate the actual intent if the pointer refers to the array or the type of its elements.

    
10.01.2017 / 11:36