How to read this variable declaration?

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

Ok, I start with: q is a pointer to a function that takes ... Not sure what should follow, but maybe this ... a pointer to a function that takes nothing and returns int and returns a pointer to the function, which takes nothing and returns int.

+4
source share
4 answers

Find the lefmost identifier, then follow your path, remembering that *a[] is an array of pointers, (*a)[] is a pointer to an array, *f() is a function that returns a pointer, and (*f)() is a pointer to a function. Remember that in the prototype you only need to specify the type of parameter; int f( int ); declares f as a function that takes one 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.

In this way:

  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 
+1
source

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

declare q as a pointer to a function (pointer to a function return int), returning a pointer to a function that returns int

+2
source

You're right.

q - a pointer point for a function that passes a pointer to a function (passing nothing returning int), returns a pointer to a function (passing nothing returning int).

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

+1
source

In accordance with the priority and associativity of the operators in the programming language C. You can understand this in the following steps:

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

1: q - pointer

2: q is a function pointer, the function pointed to by a point has an int (*) () parameter, which is also a function pointer, indicates that the function has no parameters, and the return type is int.

3: q is the function pointer, the function pointed to by the pointer has an int (*) () parameter, which is also a function pointer, indicates that the function has no parameters, and the return type is int. And also the function q points to has a return type: pointer.

4: q is a pointer to a function, the function pointed to by the pointer has an int (*) () parameter, which is also a function pointer, indicates that the function has no parameters, and the return type is int. And also the function pointed to by q is of type return: pointer (this pointer also points to a function that has no parameter).

5: q is a pointer to a function, the function pointed to by the pointer has an int (*) () parameter, which is also a function pointer, indicates that the function has no parameters, and the return type is int. Also, the function pointed to by q is of type return: pointer (this pointer also points to a function that has no parameter, and the return type of the function is int).

+1
source

Source: https://habr.com/ru/post/1486117/


All Articles