typedef

来源:互联网 发布:淘宝买正规保健品 编辑:程序博客网 时间:2024/06/04 08:34

typedef的作用是为一种数据类型定义一个新的名字,目的在于见名知意,容易理解其含义。

如:typedef unsigned int u32;给已知数据类型起了新的名字u32。

typedef与struct

typedef struct Age{        int age;        char *name;} Myage;
首先明确一点,struct Age作为一个整体,是一个数据类型,就像int、long一样。Myage是struct age这一类型的别名,就像上面的u32,我们可以使用Myage来定义一个变量,Myage age1,但是不能用Age来定义变量。

typedef struct list{int node;plist next;}*plist;

当编绎上述代码时,会报下面的错误:



对应的行数为plist next这一行。原因在于:plist为struct list的新名字,那么在struct list本身还没有建立的时候,plist自然也不存在,所以编绎器根本不认识plist

推荐做法:

struct list{int node;struct list *next;};typedef struct list *plist;


0 0
原创粉丝点击