其中哪些是函数指针

时间:2013-10-10 14:08:55

标签: c++ pointers function-pointers

我在我的一个测验中遇到过这个问题。

下面是哪些(是)有效的函数指针声明?选择所有适用的选项。

A、void* f(int);
B、int (*f)();
C、void (*f(int , void(*)(int)))(int);
D、void (*(*f)(int))();

对我来说,我会选择最后都有一对括号的所有东西。但我不确定C和D.

1 个答案:

答案 0 :(得分:2)

根据http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html

上的“左右”规则
A、void* f(int);  f is a function taking an int para that returns a pointer to void
B、int (*f)();  f is a pointer to a function that takes no (as it's c++) para and returns int
C、void (*f(int , void(*)(int)))(int); f is a function  that takes an int and a function pointer as parameters, returning a pointer to a function that takes an int as para and returns void
D、void (*(*f)(int))(); f is a pointer to a function that takes an int as para and returns a pointer to a function that take no para and returns void.

所以B和D是你的答案