C语言typedef #
一、typedef概述 #
1.1 作用 #
为已有类型创建别名。
1.2 语法 #
c
typedef 原类型 新类型名;
二、基本用法 #
2.1 简单类型 #
c
typedef int Integer;
typedef float Real;
Integer a = 10;
Real b = 3.14;
2.2 结构体 #
c
typedef struct {
char name[20];
int age;
} Student;
Student stu = {"Alice", 20};
2.3 指针类型 #
c
typedef int* IntPtr;
typedef char* String;
IntPtr p = &a;
String str = "Hello";
2.4 数组类型 #
c
typedef int IntArray[10];
IntArray arr;
arr[0] = 1;
2.5 函数指针 #
c
typedef int (*Operation)(int, int);
int add(int a, int b) { return a + b; }
Operation op = add;
三、应用场景 #
3.1 跨平台 #
c
#ifdef _WIN32
typedef __int64 int64_t;
#else
typedef long long int64_t;
#endif
3.2 简化复杂类型 #
c
typedef void (*SignalHandler)(int);
SignalHandler handler;
四、总结 #
| 用途 | 示例 |
|---|---|
| 简化名称 | typedef int Integer; |
| 结构体 | typedef struct {…} Name; |
| 指针 | typedef int* IntPtr; |
| 函数指针 | typedef int (*Func)(int); |
下一步,让我们学习类型限定符!
最后更新:2026-03-26