2013-10-10 79 views
-5

我在我的一个测验中遇到了这个问题。其中哪些是函数指针

下面是哪些有效的函数指针声明?选择所有符合条件的。

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

对我来说,我会选择所有有一对括号的结尾。但我不知道C和D.

+2

尝试[cdecl.org]( http://cdecl.org/)。 –

+0

如果您不确定,只需测试一下。 – bkausbk

+0

我必须承认,无论何时我使用函数指针(并且多年以来,我都没有),我会google他们的语法。这对我来说看起来有些模糊。我更喜欢** function_pointer **是每个函数指针声明中出现的关键字。但那只是我。 –

回答

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是你的答案

+0

你使用了你给的规则吗? – ST3

+0

我使用了左右规则。我之前不知道cdecl.org,但现在看起来很棒的网页 – tristan