最后更新于: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
✏️ 练习:动态创建数组并排序
题目 :动态分配一个长度为 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 (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 #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->score 结构体数组: struct Student class [50 ]; class [0 ].id = 1 ;typedef 简化: typedef struct { int id; char name[20 ]; float score; } Student; Student s1;
✏️ 练习:学生成绩管理
题目 :定义学生结构体(学号、姓名、成绩),输入 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 #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 ; }else if (s1->score>s2->score){ return -1 ; }else { return 0 ; } }int main () { int n=0 ; printf ("请输入学生人数:\n" ); scanf ("%d" ,&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 ; }