typedef

1 int inc(int a) 2 { 3 return(++a); 4 } 5 int multi(int*a,int*b,int*c) 6 { 7 return(*c=*a**b); 8 } 9 typedef int(FUNC1)(int in); 10 typedef int(FUNC2) (int*,int*,int*); 11 12 void show(FUNC2 fun,int arg1, int*arg2) 13 { 14 FUNC1 * p=&inc; 15 int temp =p(arg1); 16 fun(&temp,&arg1, arg2); 17 printf("%d\n",*arg2); 18 } 19 20 main( ) 21 { 22 int a; 23 show(multi,10,&a); 24 return 0; 25 }

1 typedef int(FUNC1)(int in); 2 定义了一个函数类型 FUNC1,该函数类型参数是int in,返回值是int型! 3 4 #include<stdio.h> 5 int inc(int a) 6 { 7 return(++a); //计算a+1 8 } 9 int multi(int*a,int*b,int*c) 10 { 11 return(*c=*a**b); //返回a×b 12 } 13 typedef int(FUNC1)(int in); //定义函数 14 typedef int(FUNC2) (int*,int*,int*); 15 16 void show(FUNC2 fun,int arg1, int*arg2) 17 { 18 FUNC1* p=&inc; //这里应该是你复制的有错误,定义函数指针p,指向inc。 19 int temp =p(arg1); //arg1=10 所以temp=10+1=11 20 fun(&temp,&arg1, arg2); //fun就是multi函数,计算*arg2=11*10=110 21 printf("%d\n",*arg2);//这里输出110 22 } 23 main() 24 { 25 int a; 26 show(multi,10,&a); 27 return 0; 28 }
have a series of functions with the same prototype, say
int func1(int a, int b) {
// ...
}
int func2(int a, int b) {
// ...
}
// ...
Now, I want to simplify their definition and declaration. Of course I could use a macro like that:
#define SP_FUNC(name) int name(int a, int b)
But I'd like to keep it in C, so I tried to use the storage specifier typedef
for this:
typedef int SpFunc(int a, int b);
This seems to work fine for the declaration:
SpFunc func1; // compiles
but not for the definition:
SpFunc func1 {
// ...
}
which gives me the following error:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Is there a way to do this correctly or is it impossible? To my understanding of C this should work, but it doesn't. Why?
Note, gcc understands what I am trying to do, because, if I write
SpFunc func1 = { /* ... */ }
it tells me
error: function 'func1' is initialized like a variable
Which means that gcc understands that SpFunc is a function type.
You cannot define a function using a typedef for a function type. It's explicitly forbidden - refer to 6.9.1/2 and the associated footnote:
The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.
The intent is that the type category in a function definition cannot be inherited from a typedef:
typedef int F(void); // type F is "function with no parameters // returning int" F f, g; // f and g both have type compatible with F F f { /* ... */ } // WRONG: syntax/constraint error F g() { /* ... */ } // WRONG: declares that g returns a function int f(void) { /* ... */ } // RIGHT: f has type compatible with F int g() { /* ... */ } // RIGHT: g has type compatible with F F *e(void) { /* ... */ } // e returns a pointer to a function F *((e))(void) { /* ... */ } // same: parentheses irrelevant int (*fp)(void); // fp points to a function that has type F F *Fp; //Fp points to a function that has type F
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!