指针进阶

最后更新于:2026年3月11日 下午

第2讲:指针进阶(指针与动态内存)

🔑 知识点讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
动态内存分配(需 #include <stdlib.h>):

malloc:分配内存
int *p = (int *)malloc(n * sizeof(int));

calloc:分配并初始化为0
int *p = (int *)calloc(n, sizeof(int));

realloc:重新调整大小
p = (int *)realloc(p, new_size * sizeof(int));

free:释放内存
free(p);
p = NULL; // 避免野指针

⚠️ 常见错误:
1. 忘记 free -> 内存泄漏
2. free 后继续使用 -> 野指针(悬挂指针)
3. 重复 free -> 未定义行为
4. malloc 返回 NULL 未检查

指针的指针(二级指针):
int a = 10;
int *p = &a;
int **pp = &p;
**pp == 10 // true

✏️ 练习:动态创建数组并排序

题目:动态分配一个长度为 n 的数组,输入数据后排序输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b) {
return (*(int *)a) - (*(int *)b);
}

int main() {
int n;
scanf("%d", &n);

// 动态分配
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("内存分配失败\n");
return 1;
}

for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

// 排序(使用库函数 qsort)
qsort(arr, n, sizeof(int), cmp);

for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

// 释放内存
free(arr);
arr = NULL;

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 动态分配一个长度为 n 的数组,输入数据后排序输出。
#include <stdio.h>
#include<stdlib.h>
int main(){
int n=0;//用于分配数组大小
printf("请输入数组大小:\n");
scanf("%d",&n);
int *arr=(int *)malloc(sizeof(int) * n);
printf("请输入数据:\n");
for(int i=0;i<n;i++){
scanf("%d",arr+i);
}
//冒泡排序
for(int k=0;k<n;k++)
{
for (int i = 0; i < n-1-k; i++)
{
if(arr[i]>arr[i+1]){
int temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}

}
printf("排序后的数组为:\n");
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
free(arr);
return 0;
}

第3讲:结构体

🔑 知识点讲解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
结构体可以将不同类型的数据组合在一起。

定义:
struct Student {
int id;
char name[20];
float score;
};

使用:
struct Student s1 = {1, "张三", 90.5};
struct Student s2;
s2.id = 2;
strcpy(s2.name, "李四"); // 字符串不能直接=
s2.score = 85.0;

结构体指针:
struct Student *p = &s1;
p->id // 等价于 (*p).id
p->score // 箭头运算符

结构体数组:
struct Student class[50];
class[0].id = 1;

typedef 简化:
typedef struct {
int id;
char name[20];
float score;
} Student;

Student s1; // 不需要写 struct 关键字

✏️ 练习:学生成绩管理

题目:定义学生结构体(学号、姓名、成绩),输入 n 个学生信息,按成绩从高到低排序输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <string.h>

typedef struct {
int id;
char name[20];
float score;
} Student;

int main() {
int n;
scanf("%d", &n);

Student stu[100];
for (int i = 0; i < n; i++) {
scanf("%d %s %f", &stu[i].id, stu[i].name, &stu[i].score);
}

// 冒泡排序,按成绩降序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}

// 输出
printf("%-5s %-10s %-6s\n", "学号", "姓名", "成绩");
for (int i = 0; i < n; i++) {
printf("%-5d %-10s %-6.1f\n",
stu[i].id, stu[i].name, stu[i].score);
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 定义学生结构体(学号、姓名、成绩),输入 n 个学生信息,按成绩从高到低排序输出。
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
int id;
char name[100];
float score;
} students;

//定义返回参数
int cmp(const void *a,const void *b){
students *s1=(students *)a;//强制类型转换
students *s2=(students *)b;
//按照成绩从高到低排序
if(s1->score<s2->score){
return 1;
//返回1表示s1应该排在s2后面
}else if(s1->score>s2->score){
//返回-1表示s1应该排在s2前面
return -1;
}else{
return 0;
}
}

int main(){
int n=0;
printf("请输入学生人数:\n");
scanf("%d",&n);
//动态分配一个长度为 n 的学生数组,输入数据后排序输出。
students *arr=(students *)malloc(sizeof(students)*n);
printf("请输入学生信息(学号 姓名 成绩):\n");
for(int i=0;i<n;i++){
scanf("%d %s %f",&arr[i].id,arr[i].name,&arr[i].score);
}
qsort(arr,n,sizeof(students),cmp);
printf("排序后的学生信息为:\n");
for(int i=0;i<n;i++){
printf("%d %s %.2f\n",arr[i].id,arr[i].name,arr[i].score);
}
free(arr);
return 0;
}

//实例输入:
//请输入学生人数:
//3
//请输入学生信息(学号 姓名 成绩):
//1001 Alice 85.5
//1002 Bob 92.0
//1003 Charlie 78.0
//实例输出:
//排序后的学生信息为:
//1002 Bob 92.00
//1001 Alice 85.50
//1003 Charlie 78.00

指针进阶
https://xtanguser.github.io/2026/03/10/指针进阶/
作者
小唐
发布于
2026年3月10日
许可协议