LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 789|回复: 5

请教关于typedef的一个问题

[复制链接]
发表于 2006-11-7 13:08:34 | 显示全部楼层 |阅读模式
1、typedef int Myfunc(const char *,const struct stat *,int)

2、typedef int (*Myfunc)(const char *,const struct stat *,int)

这两者的区别在哪?
是不是第一种方式使得Myfunc成为一种普通类型(像int这类的)
而第二种方式使得Myfunc成为一种指针类型(像int*这类的)?
发表于 2006-11-7 14:16:23 | 显示全部楼层
第一种定义了以后没法使用吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-7 14:22:51 | 显示全部楼层
我是在《Unix环境高级编程》第99页看到的(能不能正确运行,我没试过)
typedef int Myfunc(const char *,const struct star *,int);
static Myfunc myfunc;
...
int main(int argc,char *argv[])
{
    ...
    ret = myftw(argv[1],myfunc);
   ...
}
...
static int myftw(char *pathname,Myfunc *func)
{
...
}
...
static int myfunc(const char *pathname,const struct stat *statptr,int type)
{
...
}
回复 支持 反对

使用道具 举报

发表于 2006-11-7 15:35:52 | 显示全部楼层
哦,也能用
那两种没啥区别吧
其实对函数来说,函数变量和函数指针是一回事,不知道在赋值时是不是有什么区别
回复 支持 反对

使用道具 举报

发表于 2006-11-8 02:58:37 | 显示全部楼层
Post by catcher
1、typedef int Myfunc(const char *,const struct stat *,int)

2、typedef int (*Myfunc)(const char *,const struct stat *,int)

这两者的区别在哪?
是不是第一种方式使得Myfunc成为一种普通类型(像int这类的)
而第二种方式使得Myfunc成为一种指针类型(像int*这类的)?

楼主的理解是对的。
回复 支持 反对

使用道具 举报

发表于 2006-11-12 09:31:58 | 显示全部楼层
Post by x11
哦,也能用
那两种没啥区别吧
其实对函数来说,函数变量和函数指针是一回事,不知道在赋值时是不是有什么区别


不能说她们是一回事吧
函数毕竟是函数,指针毕竟是指针, 如果把函数也看作first-class对象的话(就象fp那样), 函数其实跟其他的类型(如整数)是一样的,指针是指向这些first-class对象的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表