例如:在C中,作爲參數傳遞時,`&function`和`function`有什麼區別?
#include <stdio.h>
typedef void (* proto_1)();
typedef void proto_2();
void my_function(int j){
printf("hello from function. I got %d.\n",j);
}
void call_arg_1(proto_1 arg){
arg(5);
}
void call_arg_2(proto_2 arg){
arg(5);
}
void main(){
call_arg_1(&my_function);
call_arg_1(my_function);
call_arg_2(&my_function);
call_arg_2(my_function);
}
運行此我得到如下:
> tcc -run try.c
hello from function. I got 5.
hello from function. I got 5.
hello from function. I got 5.
hello from function. I got 5.
我的兩個問題是:
- 有什麼用
(* proto)
和一個定義的函數原型之間的差異沒有定義? - 調用一個函數與參考運算符(
&
)和不帶?有什麼區別?
我不認爲有區別。不張貼爲答案,因爲我不太確定。 – JAB 2011-06-09 13:20:44
可能的重複[函數指針在C - 地址運算符「不必要的」](http://stackoverflow.com/questions/258422/function-pointers-in-c-address-operator-unnecessary) – outis 2012-01-24 03:48:57