C/C++结构体总结
1 | #include"iostream" |
2 | using namespace std; |
3 | |
4 | struct TestStruct |
5 | { |
6 | int iNumber; |
7 | char charArray[10]; |
8 | char ch; |
9 | double dNumber; |
10 | }; |
11 | //【嵌套的结构体类型成员】 |
12 | struct Date |
13 | { |
14 | int Day; |
15 | int Month; |
16 | int Year; |
17 | } ; |
18 | struct Person /*定义结构体*/ |
19 | { |
20 | char Name[20]; |
21 | int Age; |
22 | struct Date Birthday; //嵌套Date结构体类型成员 |
23 | } ; |
24 | //【结构体中的指针成员】 |
25 | struct Student{ |
26 | char *name; |
27 | int score; |
28 | struct Student* next; |
29 | }; |
30 | |
31 | int main(int argc, char* argv[]) |
32 | { |
33 | //结构体的大小【结构体对齐】 |
34 | //A: 结构体变量的首地址能够被其最宽基本类型成员的大小所整除 |
35 | //B: 结构体每个成员相对于结构体首地址的偏移量都是成员自身大小的整数倍,如有需要编译器会在成员之间加上填充字节 |
36 | //C:结构体的总大小为结构体最宽基本类型成员大小的整数倍 |
37 | cout<<"Size of TestStruct "<<sizeof(TestStruct)<<endl; |
38 | cout<<"Size of Date "<<sizeof(Date)<<endl; |
39 | cout<<"Size of Person "<<sizeof(Person)<<endl; |
40 | |
41 | //【结构体变量】 |
42 | TestStruct t ={5,"bcdefg",'a',1.1}; |
43 | cout<<"TestStruct ch:"<<t.ch<<" charArray:"<<t.charArray<<" dNumber:"<<t.dNumber<<" iNumber:"<<t.iNumber<<endl; |
44 | |
45 | //【结构体数组】 |
46 | TestStruct tt[3] ={{8,"bcdefg",'m',1.2},{6,"hijklm",'b',2.2},{7,"nopqrs",'c',3.3}}; |
47 | for (int i=0;i<3;i++) |
48 | { |
49 | cout<<"TestStruct["<<i<< "]ch:"<<tt[i].ch<<" charArray:"<<tt[i].charArray<<" dNumber:"<<tt[i].dNumber<<" iNumber:"<<tt[i].iNumber<<endl; |
50 | } |
51 | //【指针变量与结构体变量】 |
52 | //必须要给结构体指针变量赋予一个有效的结构体变量地址,才能正常操作结构体指针变量。 |
53 | //TestStruct *p =&t;否则将出现不可预知的问题。(*p).ch 等价于 p->ch |
54 | TestStruct *p =&t; |
55 | cout<<"TestStruct p->ch:"<<p->ch<<" p->charArray:"<<p->charArray<<" p->dNumber:"<<p->dNumber<<" p->iNumber:"<<p->iNumber<<endl; |
56 | //【指向结构体数组的指针】 |
57 | //pp的初值为tt,即指向第一个元素,则pp加1后pp就指向下一个元素的起始地址(即tt[1]得起始地址)。 |
58 | TestStruct *pp ; |
59 | for(pp =tt;pp<tt+3;pp++) |
60 | { |
61 | cout<<"TestStruct pp->ch:"<<pp->ch<<" pp->charArray:"<<pp->charArray<<" pp->dNumber:"<<pp->dNumber<<" pp->iNumber:"<<pp->iNumber<<endl; |
62 | } |
63 | //【嵌套的结构体类型成员】 |
64 | //访问嵌套结构体Date的成员 per->Birthday.Year 等价于 (*per).Birthday.Year |
65 | Person *per; |
66 | per = new Person; //per=(Person *)malloc(sizeof(Person)); |
67 | cout<<"Input name,age,year,month,day"<<endl; |
68 | cin>>per->Name>>per->Age>>per->Birthday.Year>>per->Birthday.Month>>per->Birthday.Day; |
69 | cout<<"Name:"<<per->Name<<" Age:"<<per->Age<<" Birthday:"<<(*per).Birthday.Year<<"/"<<per->Birthday.Month<<"/"<<per->Birthday.Day<<endl; |
70 | delete per; //free(per); |
71 | //【结构体中的指针成员】 |
72 | Student stu,*Pstu; |
73 | //结构体成员指针需要初始化 |
74 | stu.name = new char;//stu.name = (char*)malloc(sizeof(char)); |
75 | strcpy(stu.name,"ddd"); |
76 | stu.score = 99; |
77 | //结构体指针需要初始化 |
78 | Pstu = new Student;//Pstu = (struct Student*)malloc(sizeof(struct Student)); |
79 | //构体指针的成员指针同样需要初始化 |
80 | Pstu->name = new char;//Pstu->name = (char*)malloc(sizeof(char)); |
81 | stu.next = Pstu; |
82 | strcpy(Pstu->name,"cccc"); |
83 | Pstu->score = 88; |
84 | Pstu->next = NULL; |
85 | cout<<"stu.name: "<<stu.name<<" stu.score: "<<stu.score<<endl; |
86 | cout<<"stu.next->name: "<<stu.next->name<<" stu.next->score: "<<stu.next->score<<endl; |
87 | cout<<"Pstu->name: "<<Pstu->name<<" Pstu->score: "<<Pstu->score<<endl; |
88 | delete Pstu; |
89 | |
90 | return 0; |
91 | |
92 | } |
93 |
既然选择了远方,就不顾风雨兼程
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步