How to declare a function that returns a pointer to a function that returns a pointer to a function without using typedef in C?

I was wondering how I write a function that returns a pointer to a function that returns a pointer to a function, without a typedef. For example, a function that returns a pointer to a function may be defined as:

type (*f_name(..))(..){..} 

So this returns a pointer to a function that returns a โ€œtypeโ€, now how do you declare a function if the โ€œtypeโ€ is a pointer to a function. But since my supervisor does not want to use typedefs, I cannot use them.

Thanks for any help you can give.

+6
source share
2 answers

For such questions, there is a useful utility called cdecl ( http://cdecl.org/ ) that translates between English and C. declarations.

for instance

 cdecl> declare f as function (int) returning pointer to function (char) returning pointer to function (void) returning long 

gives an answer

 long (*(*f(int ))(char ))(void ) 

and in the other direction

 cdecl> explain int (*(*g(float))(double, double))(char) 

gives

declare g as a function (float), returning a pointer to a function (double, double) returning a pointer to a function (char), returning int

+7
source

First we write a function that takes an int and returns a float.

 float First( int f ) { return ( float )f ; } 

Then we write a function that takes nothing and returns a pointer to a function that takes an int and returns a float.
This pointer is of the same type as First .

 float ( *Second( void ) )( int ) { float (*f)( int ) = First ; return f ; } 

Finally, we write a function that takes a char and returns a pointer to a function that takes nothing and returns a pointer to a function that takes an int and returns a float. This pointer is of the same type as Second .

 float ( *( *Third( char c ) )( void ) )( int ) { ( void )c ; float ( *(*s)( void ) )( int ) = Second ; return s ; } 

If you put the prototypes on top of each other, the strange syntax starts to make sense:

 float First ( int ) ; float ( *Second ( void ) )( int ) ; float ( *( *Third( char ) )( void ) )( int ) ; 

The only difference from returning a non-functional pointer is that the parameters of the function pointers go at the end of the declaration, and the brackets are:

  type* Name( void ) ; function_type (*Name( void ) )( int ) ; 
+3
source

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


All Articles