简体   繁体   中英

How to declare a function return a pointer to function return pointer to int[3]

I'm trying the following declarations:

int (*(*((*foo)(const void *))()))[3];

and

int (*(*(*foo)(const void *)()))[3];

But the compiler gives me an error:

error: 'foo' declared as function returning a function

DEMO

Is it possible at all in c++?

Like this:

int (*(*f())())[10];

or even cleaner (kinda):

using array_type = int (*)[10];
using return_type = array_type (*)();

return_type f();

Use cdecl .

cdecl> declare f as function returning pointer to function returning pointer to array 3 of int
int (*(*f())())[3]

The way that derived declarations work is that you replace the identifier in the declaration with the new thing you are deriving. For example in the first step here, to get from "pointer to int[3]" to "function returning pointer to int[3]", we take the declaration for "pointer to int[3]", and change the identifier to be a function declarator.

A pointer to int[3]: int (*name)[3];

A function returning that: int (* name() )[3];

A pointer to that: int (* (*name) () )[3] - parentheses required otherwise the * binds to the other * instead of to name

A function returning that: int (* (* name() ) () )[3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM