结构

定义结构:定义结构,必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型。访问结构体里面的成员的时候我们用 . 这个成员访问符。

1 struct type_name   //关键字struct
2  {
3     type1    name1;  //定义类型,如 int ID;
4     type2    name2;
5     type3    name3;
6 .
7 .
8 } object_names;   //这是一个可选的结构变量

举个栗子:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 struct books
 6 {
 7     int ID;
 8     char name[100];
 9     char code[100];
10     char auther[100];
11 } bookthree ;    //定义变量
12 
13 int main()
14 {
15     books bookone;    //注意这里是变量,跟bookthree一样的效果
16     books booktwo;      //定义结构体类型books的变量booktwo
17     
18     bookone.ID = 24615;
19     strcpy(bookone.name, "雏田");
20     strcpy(bookone.code, "CX520");
21     strcpy(bookone.auther, "火影忍者");
22 
23     bookone.ID = 24520;
24     strcpy(booktwo.name, "");
25     strcpy(booktwo.code, "CD520");
26     strcpy(booktwo.auther, "火影忍者");
27     
28     strcpy(bookthree.name, "止水");
29     strcpy(bookthree.auther, "火影忍者");
30 
31     cout << "第一本书的名字:" << bookone.name << endl;
32     cout << "第一本书的作者:" << bookone.auther << endl <<endl;
33     cout << "第二本书的名字:" << booktwo.name << endl;
34     cout << "第二本书的作者:" << booktwo.auther << endl <<endl;
35     cout << "第三本书的名字:" << bookthree.name << endl;   
36     cout << "第三本书的作者:" << bookthree.auther << endl ;
37     return 0;
38 }

结果为:

第一本书的名字:雏田
第一本书的作者:火影忍者

第二本书的名字:鼬
第二本书的作者:火影忍者

第三本书的名字:止水
第三本书的作者:火影忍者

 结构作为参数:可以把结构作为函数参数,传参方式与其他类型的变量或指针类似。举个栗子:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 struct books   //结构体books
 6 {
 7     int Id;
 8     char Name[100];
 9     int Date;
10     char Auther[100];
11 } book;
12 
13 void temp(struct books book)   //结构作为参数传递,跟变量一样到用法
14 {
15     cout << "书名:" << book.Name << endl;
16     cout << "作者:" << book.Auther << endl;
17     cout << "日期:" << book.Date << endl;
18 }
19 
20 int main()
21 {
22     book.Id = 24520;       //这个地方进行结构的赋值
23     strncpy(book.Name, "伊塔奇", sizeof("伊塔奇") - 1);
24     strncpy(book.Auther, "火影忍者11", 10);
25     book.Date = 20190528;
26     temp(book);
27     return 0;
28 }

 结果为:

书名:伊塔奇
作者:火影忍者11
日期:20190528

 指向结构的指针:定义指向结构的指针,方式与定义指向其他类型变量的指针相似,如: struct Books *book1; 结构变量的地址: book1 = &Book1; 为了使用指向该结构的指针访问结构的成员,您必须使用 -> 运算符,如: book1->ID 举个栗子:

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 struct books   //结构体books
 6 {
 7     int Id;
 8     char Name[100];
 9     int Date;
10     char Auther[100];
11 } book;
12 
13 void temp(struct books *book)   //结构变量的地址作为参数传递 注意这里的struct可以省略
14 {
15     cout << "书名:" << book->Name << endl;  //这里使用->来访问结构成员
16     cout << "作者:" << book->Auther << endl;
17     cout << "日期:" << book->Date << endl;
18 }
19 
20 int main()
21 {
22     book.Id = 24520;       //这个地方进行结构的赋值
23     strncpy_s(book.Name, "伊塔奇", sizeof("伊塔奇") - 1);
24     strncpy_s(book.Auther, "火影忍者11", 10);
25     book.Date = 20190528;
26     temp(&book);  //取结构体变量book的地址
27     return 0;
28 }

结果跟上一致。

原文地址:https://www.cnblogs.com/xiaodangxiansheng/p/10933947.html