2013-10-10 24 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