int (*ptr)[10];
I was expecting ptr to be an array of pointers of 10 integers.
I'm not understanding how it is a pointer to an array of 10 integers.
int (*ptr)[10];
I was expecting ptr to be an array of pointers of 10 integers.
I'm not understanding how it is a pointer to an array of 10 integers.
On
ptr is of type "pointer to array of 10 int". In your declaration it is uninitialized so it doesn't point to any object. To make it point to an array:
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// initialize in the declaration:
int (*ptr) [10] = &arr;
// or after:
int (*ptr) [10];
ptr = &arr;
On
I like to read it like this: (great answers already posted)
int (*ptr) [10];
^ a pointer
^ to an array of 10
^ ints
vs
int* ptr[10];
^ array of 10
^ pointer to int
On
A declaration gives a “picture” of how a variable will be used.1 int (*ptr) [10] says “When (*ptr)[i] is used in an expression, it is an int.” From this, we can derive the type of ptr:
(*ptr)[i] is an int, (*ptr) must be an array of int.(*ptr) is an array of int, *ptr must be an array of int.*ptr is an array of int, ptr must be a pointer to an array of int.1 Kernighan and Ritchie, The C Programming Language, 1978, page 90.
On
In both expressions and declarations, the postfix operators [] and () have higher precedence than unary *. Because of this, the declarations
T *a[N]; // a is an array of pointer to T
T *f(); // f is a function returning pointer to T
are parsed as
T *(a[N]); // result of a[i] will be dereferenced
T *(f()); // result of f() will be dereferenced
If you want a pointer to an array or a pointer to a function, you must explicitly group the * operator with whatever the pointer expression will be:
T (*a)[N]; // result of *a will be subscripted
T (*f)(); // result of *f will be executed
The pointer does not have to be a simple identifier - you can have a function that returns a pointer to an array:
T (*f())[N]; // result of *f() will be subscripted
or an array of pointers to functions
T (*a[N])(); // result of *a[i] will be executed
A pointer to an array of 10
ints is meant to point to arrays of 10 elements, no more, no less, or in case you have a 2D array, to point to a given row of such array, provided the it has exactly 10 columns.An array of pointers to
int,int *ptr[10], is just that, it has 10 pointers tointand to each one you can assing an address of anint, it can be part of an array or not.Example 1:
Such pointer can be used to point to a 2D array with undetermined number of rows but a fixed number of columns.
Example 2:
Example 3:
Array of pointer vs pointer to array
An advantage of pointer to array comes when it's time to dynamically allocate/free memory:
Example with a 2D array with 4 rows and 5 columns:
On the other hand if you have an array of pointers you can have an uneven array, that is to say that the 1st row can have 10
ints but the 2nd can have 20:In the above example the 1st row of the 2D array has space for 1
int, the second for 2, the third for 3 and so on.