C语言 typedef用法
学习typedef · 难度:入门 · +10XP
C语言 typedef
typedef用于给类型起别名,让代码更简洁易读。
基本用法
// 给基本类型起别名
typedef unsigned long ulong;
ulong x=1000000; // 等同于 unsigned long x
// 给结构体起别名
typedef struct{char name[50];int score;}Student;
Student s={"张三",85}; // 不用写struct关键字常见应用
| 场景 | 示例 |
|---|---|
| 简化复杂类型 | typedef int(*FuncPtr)(int,int); |
| 平台兼容 | typedef long long int64; |
| 代码可读性 | typedef char* String; |
???? 练习
- 用typedef定义Student类型
- 创建该类型的变量并赋值