typedef用于给数据类型取别名,这在大的工程中是非常有用的,否则整个项目中就使用int char long等c中存在的数据类型进行定义变量,可读性很差。
typedef尤其是与结构体结合在一起时,将是非常方便的。
下面有一个简单的例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned int u32;
//typedef和结构体结合使用
struct MyStruct
{
int a;
int b;
};
typedef struct MyStruct2
{
int a;
int b;
}TMP;
int main(void)
{
u32 t; // unsigned int
//定义结构体变量,一定要加上struct关键字
struct MyStruct s1;
//MyStruct s2; //err
TMP s3;
struct MyStruct2 s4;
char buf[1024];
strcpy(buf, "1111111111");
printf("\n");
system("pause");
return 0;
}
typedef unsigned int u32;
将unsigned int定义为u32,这样比较见到这个缩写,大概的意思就知晓了。
typedef struct MyStruct2
{
int a;
int b;
}TMP;
将struct MyStruct2 结构类型定义为TMP,这样两个单词变为一个,j再定义变量的是时候,直接使用TMP定义即可。
即: TMP t1。
以上这些小的知识点,看起来很不起眼,但是一点一点累积,会发现在大的项目中,这些东西都会用上,这样就可以让我们知其然也知其所以然!
580

被折叠的 条评论
为什么被折叠?



