1

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++?

T.C.
  • 123,516
  • 14
  • 264
  • 384
  • Are you asking for C or C++; dual tagged questions get short shrift. Since you mention C++, I've removed the C tag (even though there's a decent chance in this case that the answer is much the same). The languages are different; in many respects, quite different. – Jonathan Leffler Sep 15 '14 at 04:31
  • @JonathanLeffler I thought `c++` as `c` also use cdecl, doesn't it? –  Sep 15 '14 at 04:31
  • 2
    You can't have an array as the return type of a function What do you mean by "return int[3]"? – Brian Bi Sep 15 '14 at 04:32
  • You cannot return arrays from functions; you can return pointers to arrays, though. – Jonathan Leffler Sep 15 '14 at 04:32
  • @JonathanLeffler Fixed. –  Sep 15 '14 at 04:34
  • 3
    As in, `int (*(*foo())(const void *))[3]`? – T.C. Sep 15 '14 at 04:34
  • @T.C. Perfect, thanks! –  Sep 15 '14 at 04:35
  • @JonathanLeffler C and C++ are not that different. Remember that C++ was originally implemented by Stroustrup as a preprocessor for C, which is why they share a great many things: scope rules, declaration syntax and semantics, all of C's base types, etc. etc. etc. Sure, C++ adds a great deal more, but for 99.9% of cases, a valid C program is also a valid C++ program. – dgnuff Sep 15 '14 at 04:51

3 Answers3

8

Like this:

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

or even cleaner (kinda):

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

return_type f();
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
  • Why you use using instead of typedef? –  Sep 15 '14 at 04:42
  • 3
    @DmitryFucintv - it is c++11 syntax - see [this question](http://stackoverflow.com/questions/10747810/what-is-the-difference-between-typedef-and-using-in-c11) – Steve Lorimer Sep 15 '14 at 04:43
  • @SteveLorimer Why does `typedef int(*)[10] array_type;` throw a compile-time error? http://coliru.stacked-crooked.com/a/97b34742374d9008 –  Sep 15 '14 at 04:51
  • 1
    You're using the wrong syntax for function pointer typedefs. The correct syntax is `typedef int (*array_type)[10];` – Steve Lorimer Sep 15 '14 at 04:59
5

Use cdecl.

cdecl> declare f as function returning pointer to function returning pointer to array 3 of int
int (*(*f())())[3]
Brian Bi
  • 91,815
  • 8
  • 136
  • 249
  • http://cdecl.ridiculousfish.com/?q=declare+f+as+function+returning+pointer+to+function+returning+pointer+to+array+3+of+int – phuclv Sep 15 '14 at 05:00
5

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]

M.M
  • 130,300
  • 18
  • 171
  • 314