[問題] union中的函數指標傳遞struct
開發平台(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
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章