[問題] Function pointer

作者: IhateOGC (我討厭)   2014-08-05 21:38:04
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
gcc
問題(Question):
為何ptr 餵入function而非 function address這樣也能?
#incldue <stdio.h>
typedef int (*funcp)();
int foo1(){
printf("Hello World! \n");
}
int main(){
funcp ptr;
ptr = fool;
ptr();
getchar();
return 0;
}
餵入的資料(Input):
ptr = fool;
ptr = &fool;
預期的正確結果(Expected Output):
ptr = fool; 應該compiler fail..卻compiler過了
程式碼(Code):(請善用置底文網頁, 記得排版)
補充說明(Supplement):
作者: azureblaze (AzureBlaze)   2014-08-05 21:39:00
可自動轉型
作者: CaptainH (Cannon)   2014-08-05 21:57:00
不是int, 是 function
作者: suhorng ( )   2014-08-05 23:43:00
foo1 會被自動轉成函數指標; #1ECoOqLM
作者: IhateOGC (我討厭)   2014-08-05 23:50:00
thx
作者: PUTOUCHANG (自己的廢文自己發)   2014-08-05 23:51:00
可是我記得以前寫 ptr = fool 執行時會 hang 住ptr = &fool 可以正確執行
作者: damody (天亮damody)   2014-08-06 00:20:00
一個是1一個是l你在筆誤嗎?

Links booklink

Contact Us: admin [ a t ] ucptt.com