C语言结构体嵌套
用老师的话来说就是剥洋葱(比较简单)
- 变量方式访问: .
- 指针的方式访问:->
- 或者直接包含
结构体数组定义
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//和基本数据类型定义方式是一样的 #include <stdio.h> #include <stdlib.h> struct MM { char name[20]; int age; }mmArray[3]; struct MM g_array[3]; int main() { struct MM array[3]; 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#include <stdio.h> #include <string.h> #include <stdlib.h> struct MM { char name[20]; int age; }mmArray[3]= { {"小芳",18},{"小美",28},{"小花",27} }; struct MM g_array[3]= { {"小芳",18},{"小美",28},{"小花",27} }; int main() { //创建时候做初始化 struct MM array[3] = { {"小芳",18},{"小美",28},{"小花",27}}; memset(array, 0, sizeof(struct MM) * 3); //循环初始化 struct MM temp[3]; for (int i = 0; i < 3; i++) { scanf_s("%s%d", temp[i].name,20, &temp[i].age); } 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 <string.h> #include <stdlib.h> struct MM { char name[20]; int age; }mmArray[3]= { {"小芳",18},{"小美",28},{"小花",27} }; struct MM g_array[3]= { {"小芳",18},{"小美",28},{"小花",27} }; int main() { //小芳 18 //创建时候做初始化 struct MM array[3] = { {"小芳",18},{"小美",28},{"小花",27}}; memset(array, 0, sizeof(struct MM) * 3); //循环初始化 struct MM temp[3]; for (int i = 0; i < 3; i++) { scanf_s("%s%d", temp[i].name,20, &temp[i].age); } for (int i = 0; i < 3; i++) { printf("%st%dn", temp[i].name, temp[i].age); } for (int i = 0; i < 3; i++) { printf("%st%dn", (temp+i)->name, (temp + i)->age); } 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#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> struct MM { char name[20]; int age; }; void testFirst() { struct MM mm = { "小美",19 }; struct MM* pmm = NULL; pmm = &mm; printf("%st%dn", pmm->name, pmm->age); struct MM array[3] = { {"小美",19},{"小美2",28},{"小美3",23} }; pmm = array; for (int i = 0; i < 3; i++) { printf("%st%dn", pmm[i].name, pmm[i].age); } } int main() { testFirst(); 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#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> struct MM { char name[20]; int age; }; void testSecond() { //申请一个变量内存 struct MM* pmm = (struct MM*)malloc(sizeof(struct MM)); assert(pmm); strcpy_s(pmm->name, 20, "小芳"); pmm->age = 19; printf("%st%dn", pmm->name, pmm->age); free(pmm); pmm = NULL; //申请一段内存 struct MM* pArray = (struct MM*)malloc(sizeof(struct MM) * 3); assert(pArray); for (int i = 0; i < 3; i++) { strcpy_s(pArray[i].name,20, "美美"); pArray[i].age = 19; printf("%st%dn", (pArray+i)->name, (pArray+i)->age); } free(pArray); pArray = NULL; } int main() { testSecond(); 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#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> struct MM { char name[20]; int age; }; struct Info { int num; struct MM* pmm; }; void testThird() { struct Info info = { 1001,NULL}; struct MM mm = { "小妹",18 }; struct Info info2 = { 1002,&mm }; struct Info* pinfo = (struct Info*)malloc(sizeof(struct Info)); assert(pinfo); pinfo->num = 1004; pinfo->pmm = (struct MM*)malloc(sizeof(struct MM)); assert(pinfo->pmm); strcpy_s(pinfo->pmm->name, 20, "小小"); pinfo->pmm->age = 29; printf("%dt%st%dn", pinfo->num, pinfo->pmm->name, pinfo->pmm->age); } int main() { testFirst(); testSecond(); testThird(); return 0; }
最后
以上就是清爽枫叶最近收集整理的关于【学习生涯】结构体数组的全部内容,更多相关【学习生涯】结构体数组内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复