c语言中typedef的用法

typedef是为现有的类型起一个别名,使使用起来更加的方便,注意一点,它并没有产生新的类型。

typedef int BOOL;为int型起了一个新的别名BOOL。例如下边的代码,BOOL为int的别名,然后就可以直接使用了。

typedef int BOOL;
#define TRUE 1
#define FALSE 0
BOOL flag = TRUE;

在结构体中的用法

typedef struct student{
	char cName[20];//姓名
	int iNumber;//电话号码
	struct student *next;//指向下一个节点
} LinkList;
LinkList *head;

以上定义了一个新的结构体student,并将结构体起了一个新的别名LinkList。其实不用typedef也是可以的,如下边的代码

struct student{
	char cName[20];//姓名
	int iNumber;//电话号码
	struct student *next;//指向下一个节点
};
struct student *head;

不用typedef,定义变量时需要加上struct student,比较麻烦。

关于结构体指针,如下代码

typedef struct student{
	char cName[20];//姓名
	int iNumber;//电话号码
	struct student *next;//指向下一个节点
}*LinkList;
LinkList head;

这个是定义 struct student{}*的别名为LinkList,而不是struct student{}的别名为*LinkList,所以下边定义指针可以直接这样使用。


版权声明:本文为u013252047原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。