
1.定义结构体变量的3种方式
1> 先定义类型,再定义变量(分开定义)
struct Student
{
int age;
};
struct Student stu;
2> 定义类型的同时定义变量
struct Student
{
int age;
} stu;
struct Student stu2;
3> 定义类型的同时定义变量(省略了类型名称)
struct
{
int age;
} stu;
2.结构体类型的作用域
1> 定义在函数外面:全局有效(从定义类型的那行开始,一直到文件结尾)
2> 定义在函数(代码块)内部:局部有效(从定义类型的那行开始,一直到代码块结束)
3.代码
1>结构体
1 /*
2 数组:只能由多个相同类型的数据构成
3
4 结构体:可以由多个不同类型的数据构成
5 */
6 #include <stdio.h>
7
8 int main()
9 {
10 //int ages[3] = {[2] = 10, 11, 27};
11
12
13 //int ages[3] = {10, 11, 29};
14
15 // 1.定义结构体类型
16 struct Person
17 { // 里面的3个变量,可以称为是结构体的成员或者属性
18 int age; // 年龄
19 double height; // 身高
20 char *name; // 姓名
21 };
22
23 // 2.根据结构体类型,定义结构体变量
24 struct Person p = {20, 1.55, "jack"};
25 p.age = 30;
26 p.name = "rose";
27
28 PRintf("age=%d, name=%s, height=%f\n", p.age, p.name, p.height);
29
30 /* 错误写法
31 struct Person p2;
32 p2 = {30, 1.67, "jake"};
33 */
34
35 struct Person p2 = {.height = 1.78, .name="jim", .age=30};
36 //p2.age = 25;
37
38 return 0;
39 }
2>结构体内存分析
1 #include <stdio.h>
2 int main()
3 {
4
5
6 return 0;
7 }
8
9 // 补齐算法
10 void test1()
11 {
12 struct Student
13 {
14 int age;// 4个字节
15
16 char a;
17
18 //char *name; // 8个字节
19 };
20
21 struct Student stu;
22 //stu.age = 20;
23 //stu.name = "jack";
24 // 补齐算法(对齐算法)
25 // 结构体所占用的存储空间 必须是 最大成员字节数的倍数
26
27 int s = sizeof(stu);
28 printf("%d\n", s);
29 }
30
31 // 结构体内存细节
32 void test()
33 {
34 // 1.定义结构体类型(并不会分配存储空间)
35 struct Date
36 {
37 int year;
38 int month;
39 int day;
40 };
41
42 // 2.定义结构体变量(真正分配存储空间)
43 struct Date d1 = {2011, 4, 10};
44
45
46 struct Date d2 = {2012, 8, 9};
47
48 // 会将d1所有成员的值对应地赋值给d2的所有成员
49 d2 = d1;
50 d2.year = 2010;
51
52 printf("%d - %d - %d\n", d1.year, d1.month, d1.day);
53
54 printf("%d - %d - %d\n", d2.year, d2.month, d2.day);
55 /*
56 printf("%p - %p - %p\n", &d1.year, &d1.month, &d1.day);
57
58 int s = sizeof(d1);
59 printf("%d\n", s);
60
61 */
62 }
3>注意点
1 #include <stdio.h>
2 // 从这行开始,一直到文件结尾,都是有效(跟全局变量一样)
3 struct Date
4 {
5 int year;
6 int month;
7 int day;
8 };
9
10 int a;
11
12 void test2()
13 {
14 struct Date
15 {
16 int year;
17 };
18 // 这里使用的是test2函数内部的struct Date类型
19 struct Date d1 = {2011};
20
21
22 // 结构体类型也是有作用域,从定义类型的那一行开始,一直到代码块结束
23 struct Person
24 {
25 int age;
26 };
27
28 struct Person p;
29
30 a = 10;
31 }
32
33 int main()
34 {
35 struct Date d1 = {2009, 8, 9};
36
37
38 test2();
39
40 // 不能使用test2函数中定义的类型
41 // struct Person p2;
42
43 return 0;
44 }
45
46 // 定义结构体变量
47 void test()
48 {
49 // 定义结构体变量的第3种方式
50 struct {
51 int age;
52 char *name;
53 } stu;
54
55 struct {
56 int age;
57 char *name;
58 } stu2;
59
60
61 /*结构体类型不能重复定义
62 struct Student
63 {
64 int age;
65 };
66
67 struct Student
68 {
69 double height;
70 };
71
72 struct Student stu;
73 */
74
75 /* 错误写法:结构体类型重复定义
76 struct Student
77 {
78 int age;
79 double height;
80 char *name;
81 } stu;
82
83 struct Student
84 {
85 int age;
86 double height;
87 char *name;
88 } stu2;c
89 */
90
91 /*
92 这句代码做了两件事情
93 1.定义结构体类型
94 2.利用新定义好的类型来定义结构体变量
95 */
96 // 定义变量的第2种方式:定义类型的同时定义变量
97 /*
98 struct Student
99 {
100 int age;
101 double height;
102 char *name;
103 } stu;
104
105 struct Student stu2;
106 */
107
108 /*
109 // 定义变量的第1种方式:
110 // 1.类型
111 struct Student
112 {
113 int age;
114 double height;
115 char *name;
116 };
117
118 // 2.变量
119 struct Student stu = {20, 1.78, "jack"};
120 */
121 }
4>结构体数组
1 int main()
2 {
3 struct RankRecord
4 {
5 int no; // 序号 4
6 int score; // 积分 4
7 char *name; // 名称 8
8 };
9 /*
10 struct RankRecord r1 = {1, "jack", 5000};
11 struct RankRecord r2 = {2, "jim", 500};
12 struct RankRecord r3 = {3, "jake",300};
13 */
14
15 //int ages[3] = {10, 19, 29};
16
17 //int ages[3];
18 // 对齐算法
19 // 能存放3个结构体变量,每个结构体变量占16个字节
20 // 72
21 /*
22 int no; // 序号 4
23 char *name; // 名称 8
24 int score; // 积分 4
25 */
26 // 48
27 /*
28 int no; // 序号 4
29 int score; // 积分 4
30 char *name; // 名称 8
31 */
32 struct RankRecord records[3] =
33 {
34 {1, "jack", 5000},
35
36 {2, "jim", 500},
37
38 {3, "jake",300}
39 };
40
41 records[0].no = 4;
42 // 错误写法
43 //records[0] = {4, "rose", 9000};
44
45 for (int i = 0; i<3; i++)
46 {
47 printf("%d\t%s\t%d\n", records[i].no, records[i].name, records[i].score);
48 }
49
50 //printf("%d\n", sizeof(records));
51
52
53 return 0;
54 }