Item 17. Dealing with Function and Array Declarators

The main confusion with pointer to function and pointer to array declarations arises because the function and array modifiers have higher precedence than the pointer modifier, so parentheses are often required.

int *f1(); // function that returns int *int (*fp1)(); // ptr to function that returns int

The same problem obtains with the high-precedence array modifier:

const int N = 12;int *a1[N]; // array of N int *int (*ap1)[N]; // ptr to array of N ints

Of course, once one can have a pointer to a function or to an array, one can have a pointer to such a pointer:

int (**ap2)[N]; // ptr to ptr to array of N intsint *(*ap3)[N]; // ptr to array of N int *int (**const fp2)() = 0; // const ptr to ...

Get C++ Common Knowledge: Essential Intermediate Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.