在c中写一个函数指针
Writing a function pointer in c
最近在看一段代码,发现一个函数指针写成:
int (*fn_pointer ( this_args ))( this_args )
我经常遇到这样的函数指针:
return_type (*fn_pointer ) (arguments);
讨论了类似的事情here:
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
有人能告诉我有什么区别以及它是如何工作的吗?
来自 cdecl(这是一个方便的破译 C 声明的辅助工具):
int (*fn_pointer ( this_args1 ))( this_args2 )
declare fn_pointer as function (this_args1) returning pointer to
function (this_args2) returning int
因此前者是一个函数,即returns指向函数的指针,而后者:
return_type (*fn_pointer ) (arguments);
是一个普通的"pointer to function".
阅读 Clockwise/Spiral Rule 文章中有关理解复杂声明的更多信息。
int (*fn_pointer ( this_args ))( this_args );
将 fn_pointer
声明为接受 this_args
和 returns 的函数的指针,该指针指向接受 this_args
作为参数且 returns 为 int
类型。相当于
typedef int (*func_ptr)(this_args);
func_ptr fn_pointer(this_args);
让我们多了解一下:
int f1(arg1, arg2); // f1 is a function that takes two arguments of type
// arg1 and arg2 and returns an int.
int *f2(arg1, arg2); // f2 is a function that takes two arguments of type
// arg1 and arg2 and returns a pointer to int.
int (*fp)(arg1, arg2); // fp is a pointer to a function that takes two arguments of type
// arg1 and arg2 and returns a pointer to int.
int f3(arg3, int (*fp)(arg1, arg2)); // f3 is a function that takes two arguments of
// type arg3 and a pointer to a function that
// takes two arguments of type arg1 and arg2 and
// returns an int.
int (*f4(arg3))(arg1, arg2); // f4 is a function that takes an arguments of type
// arg3 and returns a pointer to a function that takes two
// arguments of type arg1 and arg2 and returns an int
How to read int (*f4(arg3))(arg1, arg2);
f4 -- f4
f3( ) -- is a function
f3(arg3) -- taking an arg3 argument
*f3(arg3) -- returning a pointer
(*f3(arg3))( ) -- to a function
(*f3(arg3))(arg1, arg2) -- taking arg1 and arg2 parameter
int (*f3(arg3))(arg1, arg2) -- and returning an int
所以,最后一个家庭作业:)。试着弄清楚声明
void (*signal(int sig, void (*func)(int)))(int);
并使用typedef
重新定义它。
这个
int (*fn_pointer ( this_args1 ))( this_args2 )
声明一个函数,该函数将 this_args1
和 returns 类型的函数指针作为参数
int (*fn_pointer)(this_args2)
所以它只是一个函数,returns 一个函数指针。
最近在看一段代码,发现一个函数指针写成:
int (*fn_pointer ( this_args ))( this_args )
我经常遇到这样的函数指针:
return_type (*fn_pointer ) (arguments);
讨论了类似的事情here:
// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}
有人能告诉我有什么区别以及它是如何工作的吗?
来自 cdecl(这是一个方便的破译 C 声明的辅助工具):
int (*fn_pointer ( this_args1 ))( this_args2 )
declare fn_pointer as function (this_args1) returning pointer to function (this_args2) returning int
因此前者是一个函数,即returns指向函数的指针,而后者:
return_type (*fn_pointer ) (arguments);
是一个普通的"pointer to function".
阅读 Clockwise/Spiral Rule 文章中有关理解复杂声明的更多信息。
int (*fn_pointer ( this_args ))( this_args );
将 fn_pointer
声明为接受 this_args
和 returns 的函数的指针,该指针指向接受 this_args
作为参数且 returns 为 int
类型。相当于
typedef int (*func_ptr)(this_args);
func_ptr fn_pointer(this_args);
让我们多了解一下:
int f1(arg1, arg2); // f1 is a function that takes two arguments of type
// arg1 and arg2 and returns an int.
int *f2(arg1, arg2); // f2 is a function that takes two arguments of type
// arg1 and arg2 and returns a pointer to int.
int (*fp)(arg1, arg2); // fp is a pointer to a function that takes two arguments of type
// arg1 and arg2 and returns a pointer to int.
int f3(arg3, int (*fp)(arg1, arg2)); // f3 is a function that takes two arguments of
// type arg3 and a pointer to a function that
// takes two arguments of type arg1 and arg2 and
// returns an int.
int (*f4(arg3))(arg1, arg2); // f4 is a function that takes an arguments of type
// arg3 and returns a pointer to a function that takes two
// arguments of type arg1 and arg2 and returns an int
How to read int (*f4(arg3))(arg1, arg2);
f4 -- f4
f3( ) -- is a function
f3(arg3) -- taking an arg3 argument
*f3(arg3) -- returning a pointer
(*f3(arg3))( ) -- to a function
(*f3(arg3))(arg1, arg2) -- taking arg1 and arg2 parameter
int (*f3(arg3))(arg1, arg2) -- and returning an int
所以,最后一个家庭作业:)。试着弄清楚声明
void (*signal(int sig, void (*func)(int)))(int);
并使用typedef
重新定义它。
这个
int (*fn_pointer ( this_args1 ))( this_args2 )
声明一个函数,该函数将 this_args1
和 returns 类型的函数指针作为参数
int (*fn_pointer)(this_args2)
所以它只是一个函数,returns 一个函数指针。