C语言中typedef的使用方法
typedef是C语言中的一个关键字,用来定义新的数据类型,可以将一个已有的数据类型重新命名,方便程序员在编写程序时使用。typedef的使用方法如下:
- 定义基本数据类型的别名
typedef int INT; typedef unsigned int UINT; typedef char CHAR; typedef float FLOAT; typedef double DOUBLE;
- 定义结构体的别名
typedef struct student{ char name[20]; int age; char sex; }STUDENT;
- 定义指针类型的别名
typedef int* PINT; typedef char* PCHAR;
- 定义函数指针类型的别名
typedef int (*FUNC)(int a, int b);
- 定义复合数据类型的别名
typedef struct complex{ double real; double imag; }COMPLEX;
typedef struct student{ char name[20]; int age; char sex; struct student* next; }STUDENT;
- 使用typedef定义数组类型的别名
typedef int INT_ARRAY[10];
- 使用typedef定义枚举类型的别名
typedef enum color{ RED, BLUE, GREEN }COLOR;
使用typedef可以使程序的代码更加简洁,易于阅读和理解。
原文地址: http://www.cveoy.top/t/topic/b4fw 著作权归作者所有。请勿转载和采集!