APUE 中有段代码,关于 typedef 定义不是很理解,期待解惑谢谢。

2014-07-26 15:07:22 +08:00
 itsuibi
请问
typedef int Myfunc("const char *, const struct stat *, int");
static Myfunc myfunc;
这段代码应该怎么理解呢?

typedef int (*Myfunc)("const char *, const struct stat *, int");

有什么区别呢?

谢谢
2697 次点击
所在节点    Linux
3 条回复
wy315700
2014-07-26 19:11:45 +08:00
Myfunc 是一个函数类型

static的意思是本文件作用域
zeayes
2014-07-26 21:55:24 +08:00
typedef int Myfunc(const char *, const struct stat *, int);
// Myfunc是一个有3个参数(分别是:const char *, const struct stat *, int)且返回值是int的函数类型。
static Myfunc myfunc;
// 申明一个Myfunc类型的静态变量。

typedef int (*Myfunc)(const char *, const struct stat *, int);
// Myfunc是一个有3个参数(分别是:const char *, const struct stat *, int)且返回值是int的函数指针。
baka
2014-07-26 23:54:54 +08:00
函数名或函数指针对编译器而言,结果实际上是一样的。
以函数名调用的时候,编译器会做function to pointer conversion,这样还是会拿到函数入口指针,相当于做了&myfunc

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/124607

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX