APUE 中有段代码,关于 typedef 定义不是很理解,期待解惑谢谢。
- 0次
- 2021-07-16 13:58:38
- idczone
请问
typedef int Myfunc("const char *, const struct stat *, int");
static Myfunc myfunc;
这段代码应该怎么理解呢?
与
typedef int (*Myfunc)("const char *, const struct stat *, int");
有什么区别呢?
谢谢
Myfunc 是一个函数类型
static的意思是本文件作用域
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的函数指针。
函数名或函数指针对编译器而言,结果实际上是一样的。
以函数名调用的时候,编译器会做function to pointer conversion,这样还是会拿到函数入口指针,相当于做了&myfunc