I understand that double * price[5];
is an array of pointers. What does double (* data)[5]
mean? Where is it used for?
2 Answers
It's a pointer to an array of 5 double
s. Rarely used.
cdecl.org is useful to answer these kinds of questions for yourself:
declare data as pointer to array 5 of double

- 174,939
- 50
- 355
- 478
-
1Whit a unix-like OS, you do not need the website. Install it and call cdecl then `explain double * price[5];`. – 12431234123412341234123 Sep 15 '20 at 12:59
-
That site always was way too crude though. `int long x;` syntax error! `static const int x;` syntax error! ...and so on. It doesn't know the C syntax. – Lundin Sep 15 '20 at 13:37
It's a pointer to array or array pointer. Think of it as a pointer that can point at the array as whole rather than just at the first item of the array.
These are mainly there to keep the language type system consistent. If you have an array double price[5]
, then &price
gives the address of that array, which needs a corresponding pointer type double (*) [5]
. This type can only be used to point at an array of 5 double
, unlike a double*
which can be used to point at the first item of any array of double
. So it is less generic but more type safe.
One of the main uses for array pointers is dynamic allocation of multi-dimensional arrays. These should be allocated with a single statement like this:
int (*array) [x][y][z] = malloc( sizeof(int[x][y][z]) );
or equivalent
int (*array) [x][y][z] = malloc( sizeof *array );
That way we get a true "3D array" with all memory allocated adjacently, no heap fragmentation etc. The problem is however that array pointers are cumbersome: to use the pointer in the example above, we would have to de-reference it (like any pointer): (*array)[i][j][k] = ...
.
To avoid that burdensome syntax, we can instead create an pointer to the first element, which is an array of type int [y][z]
:
int (*array)[y][z] = malloc( sizeof(int[x][y][z]) );
...
array[i][j][k] = ... ;
where the array[i]
is pointer arithmetic referring to array number i
of type int[y][z]
, [j]
refers to an array of type int [z]
and [k]
refers to an int
.
Other uses of array pointers is increasing type safety of arrays passed to functions, or returning a pointer to an array from a function.

- 195,001
- 40
- 254
- 396