黑马程序员技术交流社区

标题: 定义结构体时,typedef的作用 [打印本页]

作者: Melody_cw    时间: 2014-7-20 02:13
标题: 定义结构体时,typedef的作用
本帖最后由 Melody_cw 于 2014-7-21 14:19 编辑

RTtypedef struct student
{
int num;
struct student *next;
}student;
struct student
{
int num;
struct student *next;
};
这俩种定义结构以后,有分别吗?详细些

作者: 为梦想挺身而出    时间: 2014-7-20 08:04
区别在于在用这个结构体定义变量的时候
第一种 用student+变量名
第二种 用struct student +变量名
作者: Melody_cw    时间: 2014-7-20 08:58
为梦想挺身而出 发表于 2014-7-20 08:04
区别在于在用这个结构体定义变量的时候
第一种 用student+变量名
第二种 用struct student +变量名 ...

也就是说typedef把定义结构体的名称给重置了。相当于把struct student给成了struct,这样理解对么
作者: 为梦想挺身而出    时间: 2014-7-20 09:43
Melody_cw 发表于 2014-7-20 08:58
也就是说typedef把定义结构体的名称给重置了。相当于把struct student给成了struct,这样理解对么 ...

typedef的作用:给已经存在的类型起一个新的名称
student是这个结构体新的名称  还是可以用struct student 定义,。struct后面的student可以省略,省略后就不能 用struct student定义了。
楼主加油,希望对你有所帮助!

作者: ⒈心只霸占沵    时间: 2014-7-20 09:56
typedef 实际上就是给类型取外号(别名)
作者: Melody_cw    时间: 2014-7-20 10:01
⒈心只霸占沵 发表于 2014-7-20 09:56
typedef 实际上就是给类型取外号(别名)

嗯 已经明白了 ,谢谢
作者: Melody_cw    时间: 2014-7-20 10:03
为梦想挺身而出 发表于 2014-7-20 09:43
typedef的作用:给已经存在的类型起一个新的名称
student是这个结构体新的名称  还是可以用struct studen ...

嗯嗯,感谢你




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2