9
What is the difference between these two statements?
int* array1[10];
int (*array2)[10];
Why are there parentheses in the second?
9
What is the difference between these two statements?
int* array1[10];
int (*array2)[10];
Why are there parentheses in the second?
10
We can see that both are arrays.
int* array1[10];
It’s a case where the array will have as data elements of type "pointers to int
". Then memory addresses will be stored in it. The value of each element is elsewhere pointed out by the data stored in the array.
int (*array2)[10];
I put in the Github for future reference.
Here the pointer is part of the array and not the type of elements of array. So we have a pointer to the array (that It’s still a pointer, but that’s another matter), so the variable array2
shall be a memory address indicating where the array is. The elements of that array will be the type int
, then the value is already within the array.
C has an 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 real intention if the pointer refers to the array or the type of elements.
Browser other questions tagged c c++ array pointer variable-declaration
You are not signed in. Login or sign up in order to post.