[英]How to use typedef for extern function in C?
我在項目中有兩個用C11編寫的模塊:“ test1.c”和“ test2.c”。 模塊“ test1.c”:
int FunctionWithVeryLONGLONGLONGLONGName(char* data)
{
// do something
}
模塊“ test2.c”:
extern int FunctionWithVeryLONGLONGLONGLONGName(char* data);
int main(void)
{
char data[ DATA_LEN + 1 ] = { "test_data" };
FunctionWithVeryLONGLONGLONGLONGName(data);
return 0;
}
我想在模塊“ test2.c”中使用函數“ FunctionWithVeryLONGLONGLONGLONGName”的簡稱,而無需修改模塊“ test1.c”。 鐵,像這樣:
FuncWithShortName(data);
我嘗試做:
extern int FunctionWithVeryLONGLONGLONGLONGName(char* data);
typedef int FunctionWithVeryLONGLONGLONGLONGName(char* data);
FunctionWithVeryLONGLONGLONGLONGName FuncWithShortName;
int main(void)
{
char data[ DATA_LEN + 1 ] = { "test_data" };
FuncWithShortName(data);
return 0;
}
但是編譯器給出了一個錯誤:“函數FunctionWithVeryLONGLONGLONGLONGName的定義需要括號。” 我做錯了什么 ?
typedef
創建類型別名,而不是函數別名或其他任何東西。
您可以通過定義函數的文件本地指針來獲得某種函數別名:
static int (*const FuncWithShortName)(char*) =
FuncWithVeryLONGLONGLONGLONGName;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.