[英]How do I declare a function that returns a pointer to a function that returns a function pointer without using a typedef in C?
I was wondering how I write a function which returns a pointer to a function which returns a function pointer, without a typedef. 我想知道如何编写一个函数,它返回一个指向函数的指针,该函数返回一个没有typedef的函数指针。 For instance a function which returns a function pointer can be define as:
例如,返回函数指针的函数可以定义为:
type (*f_name(..))(..){..}
So this returns a pointer to a function that returns a 'type', now how do you declare the function if the 'type' is a function pointer. 所以这会返回一个指向返回'type'的函数的指针,现在如果'type'是一个函数指针,你如何声明该函数。 But as my supervisor does not want typedefs used I can't use them.
但由于我的主管不想使用typedef,我无法使用它们。
Thanks for any help you can give. 谢谢你提供的所有帮助。
For questions like this there is a nifty utility called cdecl
( http://cdecl.org/ ) which translates between english and C declarations. 对于这样的问题,有一个叫做
cdecl
( http://cdecl.org/ )的漂亮工具,它可以在英语和C声明之间进行转换。
For instance 例如
cdecl> declare f as function (int) returning pointer to function (char) returning pointer to function (void) returning long
gives the answer 给出了答案
long (*(*f(int ))(char ))(void )
and, in the other direction, 而在另一个方向,
cdecl> explain int (*(*g(float))(double, double))(char)
gives 给
declare g as function (float) returning pointer to function (double, double) returning pointer to function (char) returning int
声明g作为函数(float)返回指向函数(double,double)的指针,返回指向函数(char)返回int的指针
First we write a function taking an int and returning a float. 首先我们编写一个带int的函数并返回一个float。
float First( int f )
{
return ( float )f ;
}
Then we write a function taking nothing and returning a pointer to a function taking an int and returning a float. 然后我们写一个函数,什
This pointer is the same type as First
. 该指针与
First
类型相同。
float ( *Second( void ) )( int )
{
float (*f)( int ) = First ;
return f ;
}
Finally we write a function taking a char and returning a pointer to a function taking nothing and returning a pointer to a function taking an int and returning a float. 最后,我们编写了一个函数,它接受一个char并返回一个指向函数的指针,该函数不执行任何操作,并返回一个指向函数的指针,该函 This pointer is the same type as
Second
. 该指针与
Second
类型相同。
float ( *( *Third( char c ) )( void ) )( int )
{
( void )c ;
float ( *(*s)( void ) )( int ) = Second ;
return s ;
}
If you place the prototypes along one another, the weird syntax starts to make sense: 如果你将原型放在一起,那么奇怪的语法就会有意义:
float First ( int ) ;
float ( *Second ( void ) )( int ) ;
float ( *( *Third( char ) )( void ) )( int ) ;
The only difference to returning an non-function pointer is that function pointers parameters go at the end of the declaration, and the brackets: 返回非函数指针的唯一区别是函数指针参数位于声明的末尾,括号:
type* Name( void ) ;
function_type (*Name( void ) )( int ) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.