C pointer to array/array of pointers disambiguation C pointer to array/array of pointers disambiguation arrays arrays

C pointer to array/array of pointers disambiguation


int* arr[8]; // An array of int pointers.int (*arr)[8]; // A pointer to an array of integers

The third one is same as the first.

The general rule is operator precedence. It can get even much more complex as function pointers come into the picture.


Use the cdecl program, as suggested by K&R.

$ cdeclType `help' or `?' for helpcdecl> explain int* arr1[8];declare arr1 as array 8 of pointer to intcdecl> explain int (*arr2)[8]declare arr2 as pointer to array 8 of intcdecl> explain int *(arr3[8])declare arr3 as array 8 of pointer to intcdecl>

It works the other way too.

cdecl> declare x as pointer to function(void) returning pointer to floatfloat *(*x)(void )


I don't know if it has an official name, but I call it the Right-Left Thingy(TM).

Start at the variable, then go right, and left, and right...and so on.

int* arr1[8];

arr1 is an array of 8 pointers to integers.

int (*arr2)[8];

arr2 is a pointer (the parenthesis block the right-left) to an array of 8 integers.

int *(arr3[8]);

arr3 is an array of 8 pointers to integers.

This should help you out with complex declarations.