1. 基本用法
#include <stdio.h>
// 基本类型的别名
typedef int Integer;
typedef unsigned long ulong;
typedef char* String;
int main() {
// 使用类型别名声明变量
Integer num = 42;
ulong bigNum = 123456789UL;
String str = "Hello, World!";
printf("num = %d\n", num);
printf("bigNum = %lu\n", bigNum);
printf("str = %s\n", str);
return 0;
}
2. 结构体typedef
#include <stdio.h>
#include <string.h>
// 方法1:分开定义
struct Person {
char name[50];
int age;
};
typedef struct Person Person;
// 方法2:直接在定义时使用typedef
typedef struct Student {
char name[50];
int id;
float score;
} Student;
// 方法3:匿名结构体
typedef struct {
char brand[50];
char model[50];
int year;
} Car;
int main() {
// 使用typedef后的类型名直接声明变量
Person person1;
strcpy(person1.name, "张三");
person1.age = 25;
Student student1;
strcpy(student1.name, "李四");
student1.id = 1001;
student1.score = 85.5;
Car car1;
strcpy(car1.brand, "Toyota");
strcpy(car1.model, "Camry");
car1.year = 2022;
// 打印结果
printf("Person: %s, %d岁\n", person1.name, person1.age);
printf("Student: %s, 学号%d, 成绩%.1f\n",
student1.name, student1.id, student1.score);
printf("Car: %s %s %d\n", car1.brand, car1.model, car1.year);
return 0;
}
3. 函数指针typedef
#include <stdio.h>
// 为函数指针类型创建别名
typedef int (*Operation)(int, int);
typedef void (*Printer)(const char*);
// 示例函数
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
void printMessage(const char* msg) {
printf("%s\n", msg);
}
int main() {
// 使用typedef定义的函数指针类型
Operation op1 = add;
Operation op2 = subtract;
Printer print = printMessage;
// 调用函数
printf("10 + 5 = %d\n", op1(10, 5));
printf("10 - 5 = %d\n", op2(10, 5));
print("Hello from function pointer!");
return 0;
}
4. 数组typedef
#include <stdio.h>
// 为数组类型创建别名
typedef int IntArray[5];
typedef char String[50];
typedef int Matrix[3][3];
int main() {
// 使用typedef定义的数组类型
IntArray numbers = {1, 2, 3, 4, 5};
String name = "张三";
Matrix m = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 使用数组
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
printf("Name: %s\n", name);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
return 0;
}
5. 复杂类型typedef
#include <stdio.h>
// 指针类型的typedef
typedef int* IntPtr;
typedef char* CharPtr;
// 指向函数的指针数组
typedef int (*OperationArray[4])(int, int);
// 嵌套结构体
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point start;
Point end;
} Line;
// 函数示例
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
int main() {
// 使用指针类型别名
int num = 42;
IntPtr ptr = #
printf("*ptr = %d\n", *ptr);
// 使用函数指针数组
OperationArray operations = {add, subtract, multiply, divide};
int a = 10, b = 5;
const char* op_names[] = {"加", "减", "乘", "除"};
for (int i = 0; i < 4; i++) {
printf("%d %s %d = %d\n",
a, op_names[i], b, operations[i](a, b));
}
// 使用嵌套结构体
Line line = {{0, 0}, {5, 5}};
printf("线段: (%d,%d) -> (%d,%d)\n",
line.start.x, line.start.y,
line.end.x, line.end.y);
return 0;
}
6. typedef与指针结合
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点
typedef struct Node {
int data;
struct Node* next;
} Node;
// 定义指向节点的指针类型
typedef Node* NodePtr;
// 定义指向函数的指针类型
typedef void (*NodeOperation)(Node*);
// 节点操作函数
void printNode(Node* node) {
if (node) {
printf("%d ", node->data);
}
}
// 创建新节点
NodePtr createNode(int data) {
NodePtr newNode = (NodePtr)malloc(sizeof(Node));
if (newNode) {
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
int main() {
// 创建链表
NodePtr head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
// 使用函数指针遍历链表
NodeOperation operation = printNode;
NodePtr current = head;
while (current) {
operation(current);
current = current->next;
}
printf("\n");
// 清理内存
current = head;
while (current) {
NodePtr temp = current;
current = current->next;
free(temp);
}
return 0;
}