typedef
:是c语言的一个关键字,用于给已知数据类型取一个别名
例如typedef unsigned char uint8;
此时 uint8 也就是 unsigned char
与define
的区别:
typedef
只可用于对数据类型进行取新名字,define
无此限制typedef
由编译器执行解释,而define
由预编译器执行解释
/*
@file main.c
@brief 线性结构之typedef
@author EricsT (EricsT@163.com)
@version v1.0.0
@date 2025-09-21
@history 2025-09-21 EricsT - 新建文件
*/#include <stdio.h>typedef unsigned char uint_8;//为 unsigned char 多取一个名字为 uint_8,uint_8 等价于 unsigned chartypedef struct Student
{int sid;char name[100];char sex;
}ST;// ST 等价于 Studentint main(void)
{ST st;st.sid = 200;printf("%d", st.sid);return 0;
}
/*
@file main.c
@brief 线性结构之typedef
@author EricsT (EricsT@163.com)
@version v1.0.0
@date 2025-09-21
@history 2025-09-21 EricsT - 新建文件
*/#include <stdio.h>typedef struct Student
{int sid;char name[100];char sex;
}* ptrSt;// ptrSt 等价于 Student*int main(void)
{Student st;ptrSt pst = &st;pst->sid = 100;printf("%d", st.sid);return 0;
}
/*
@file main.c
@brief 线性结构之typedef
@author EricsT (EricsT@163.com)
@version v1.0.0
@date 2025-09-21
@history 2025-09-21 EricsT - 新建文件
*/#include <stdio.h>typedef struct Student
{int sid;char name[100];char sex;
}*ptrSt, ST;// ptrSt 等价于 Student*// ST 等价于 Studentint main(void)
{ST st;ptrSt pst = &st;pst->sid = 50;printf("%d", st.sid);return 0;
}