1 #include <stdio.h>
2
3 #include <struct.h>
4
5 #include <string.h>
6
7
8
9 // struct 是结构体的关键词。
10
11 typedef struct Student
12
13 {
14
15 char name[30];
16
17 char addr[50];
18
19 int age;
20
21 }STU;// STU=struct Student
22
23
24
25
26
27
28
29 int main(int argc, const char * argv[]) {
30
31 STU student1;
32
33 STU student2;
34
35
36
37 strcpy(student1.name,"张三");
38
39 strcpy(student1.addr,"北京");
40
41 student1.age=20;
42
43
44
45 strcpy(student2.name,"李四");
46
47 strcpy(student2.addr, "四川");
48
49 student2.age=30;
50
51
52
53 printf("-------------利用指针访问结构体成员。\n");
54
55 STU *pointer1=&student1;
56
57 printf("student1.name = %s\n",pointer1->name);
58
59 printf("student1.addr = %s\n",pointer1->addr);
60
61 printf("student1.age = %d\n",pointer1->age);
62
63 STU *pointer2=&student2;
64
65 printf("student2.name = %s\n",pointer2->name);
66
67 printf("student2.addr = %s\n",pointer2->addr);
68
69 printf("student2.age = %d\n",pointer2->age);
70
71
72
73 printf("-------------利用指针访问结构体地址和结构体中成员的地址。\n");
74
75 printf("&student1 = %p\n",&student1);
76
77 printf("&(pointer->name) = %p\n",&(pointer1->name));
78
79 printf("&(pointer->name) = %p\n",&(pointer1->addr));
80
81 printf("&(pointer->name) = %p\n",&(pointer1->age));
82
83
84
85
86
87 printf("-------------利用结构体访问地址。\n");
88
89 printf("&(student1.name) = %p\n",&(student1.name));
90
91 printf("&(student1.addr) = %p\n",&(student1.addr));
92
93 printf("&(student1.age) = %p\n",&(student1.age));
94
95
96
97
98
99
100
101 return 0;
102
103 }