[問題] union中的函數指標傳遞struct

看板C_and_CPP (C/C++)作者 (NO KING)時間14年前 (2012/04/05 18:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev-C++ 4.9.9.2 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) No 問題(Question): 在程式碼中, 如果F_A不傳遞struct而只是傳一般變數的話不會有問題 而如果改傳結構就會出現warning訊息 預期的正確結果(Expected Output): 100, 2 錯誤結果(Wrong Output): Function Ob[] = {{F_A}, {F_B}}; 這行會秀 [Warning]initialization from incompatible pointer type 的警告訊息 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> typedef struct { int x; int y; }Point; int F_A(Point* p) { return 100; } int F_B(int input) { return input; } typedef union { int (*FuncA)(Point* p); int (*FuncB)(int); }Function; Function Ob[] = {{F_A}, {F_B}}; int main(int arqc, int* arqv[]) { int i = 0; Point x; printf("%d\n", Ob[0].FuncA(&x)); printf("%d\n", Ob[1].FuncB(2)); system("PAUSE"); return 0; } 補充說明(Supplement): 在FuncA中如果傳遞的參數不是用結構的話就不會有warning反之則會有, 不知道是我傳遞的方式有錯還是用法不對, 希望板上的大大能幫我解惑 先謝謝大大了~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.222.245.48
文章代碼(AID): #1FVNPh1l (C_and_CPP)
文章代碼(AID): #1FVNPh1l (C_and_CPP)