4
int (*(*q)(int (*)()))();

Ok, I start with: q is a pointer to a function, which takes... Not sure what should follow next, but perhaps it's ...a pointer to function, which takes nothing and returns int, and returns pointer to a function, which takes nothing and returns int.

user1242967
  • 1,220
  • 3
  • 18
  • 30
  • BTW you got it right. For the first try. Congrats! –  Jun 13 '13 at 21:13
  • 1
    It also says, "if you need to pass this code to anyone, pass it to Tony", because being hurt is a lot better than being skinned alive and then killed :-) – Yamodax Jun 13 '13 at 23:13
  • +1 for answering your own question on such a nightmare. – Yamodax Jun 13 '13 at 23:14
  • Its good answer: [Complex C declaration](http://stackoverflow.com/questions/15111526/complex-c-declaration/15112210#15112210) , will help you a lot! – Grijesh Chauhan Jun 14 '13 at 06:40

4 Answers4

2

The trick is that q itself is a function pointer that returns and takes a function pointer. cdecl says:

declare q as pointer to function (pointer to function returning int) returning pointer to function returning int

1

You are right.

q is a pointer point to a function passing an pointer to a function (passing nothing returning int) returning a pointer to a function (passing nothing returning int).

See here. http://c-faq.com/decl/spiral.anderson.html

Sacry
  • 535
  • 4
  • 9
1

Find the lefmost identifier, then work your way out remembering that *a[] is an array of pointer, (*a)[] is a pointer to an array, *f() is a function returning a pointer, and (*f)() is a pointer to a function. Remember that in a prototype you only need to provide the type of the parameter; int f( int ); declares f as a function that takes a single int parameter; int f( int * ); declares f as a function that takes a single int pointer as a parameter. Similarly, int f( int (*)[N] ); declares f as a function that takes a pointer to an array as a parameter. Apply these rules recursively to any parameters in the function.

Thus:

        q                  -- q
       *q                  -- is a pointer to
      (*q)(         )      --   a function
      (*q)(     *   )      --     that takes a pointer to
      (*q)(    (*)())      --       a function
      (*q)(int (*)())      --       returning int
     *(*q)(int (*)())      --   returning a pointer to
    (*(*q)(int (*)()))()   --       a function  
int (*(*q)(int (*)()))();  --       returning int
John Bode
  • 119,563
  • 19
  • 122
  • 198
1

According to Precedence and associativity of Operators in the C Programming language. You can understand it in the following steps:

int (*(*q)(int (*)()))();

q->*->(int (*)())->*->()->int
   1      2        3   4   5

1:q is a pointer

2:q is a function pointer, the function it points to has a parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int.

3:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And the function which q points to has a return type : pointer.

4:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter).

5:q is a function pointer, the function it pointers to has parameter int (*)(), which is also a function pointer, points to a function has no parameters and return type is int. And the function which q points to has a return type : pointer(this pointer also points to a function which has no parameter and the function's return type is int).

Charles0429
  • 1,406
  • 5
  • 15
  • 31