Definition: A
structure (struct) is a collection of data composed of a series of data of the same type or different types, also called a structure.
The form for declaring a struct type is:
struct Student{ //declare a structure type Student
int num; //declare an integer variable num
char name[20]; //declare a character array name
char sex; //declare a character variable sex
int age; / /declare an integer variable age
float score; //declare a single-precision variable
char addr[30]; //declare a character array addr
}
The definition method and initialization of structure type variable
The method of defining structure variables:
(1) First declare the structure type before defining the variable name
#include<iostream>
using namespace std;
int main(){
struct Student{ //declare a struct type Student
int num; //declare an integer variable num
char name[20]; //declare a character array name
char sex; //declare a character variable sex
int age; //declare an integer variable age
float score; //declare a single-precision variable
char addr[30]; //declare a character array addr
};
Student student1, student2;// Define structure type variables student1 and student2
cout<<sizeof(Student)<<endl;
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
After the structure variable is defined, the system allocates a memory unit for it. (You can use the sizeof function to view the number of bytes allocated, there are differences in different compilation systems)
(2)在声明类型的同时定义变量
#include<iostream>
using namespace std;
int main(){
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
}student1,student2;//声明变量student1和student2
cout<<sizeof(Student)<<endl;
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
(3)直接定义结构体类型变量
#include<iostream>
using namespace std;
int main(){
struct { //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
}student1,student2;//声明变量student1和student2
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
这种定义方法虽然合法,但是不常用。比较常用的是第一种方法。
关于结构体的类型要注意的几点:
(1)类型与变量是不同的概念,不要混淆。只能对结构体变量中的成员赋值,而不能对结构体类型赋值。
(2)对结构体变量中的成员(即“域”),可以单独使用,它的作用与地位相当于同类型的普通变量。
(3)结构体的成员也可以是一个结构体变量。
#include<iostream>
using namespace std;
struct Date{ //声明一个结构体类型Date
int month; //日期中的月份
int day; //日期中的天
int year; //日期中的年份
};
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
Date birthday; //Date是结构体类型,birthday是Date的类型的变量
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
int main(){
Student qianshou;
Date riqi;
cout<<sizeof(riqi)<<endl;
cout<<sizeof(qianshou)<<endl;
return 0;
}
(5)结构体中的成员名可以与程序中的变量名相同,但二者没有关系。
例如,程序中可以另定义一个整形变量,他与student中的num是两回事,互不影响。
2 结构体变量的初始化
(1)在定义结构体时对结构体变量指定初始值
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
} student1={
10001,
"qianshou",
'm',
19,
"100",
"JiNan"
};
(2)在定义变量时进行初始化(这种方法更常用)
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
Student student1={
<SPAN style="WHITE-SPACE: pre"> </SPAN> 10001,
<SPAN style="WHITE-SPACE: pre"> </SPAN> "qianshou",
<SPAN style="WHITE-SPACE: pre"> </SPAN> 'm',
<SPAN style="WHITE-SPACE: pre"> </SPAN> 19,
<SPAN style="WHITE-SPACE: pre"> </SPAN> "100",
<SPAN style="WHITE-SPACE: pre"> </SPAN> "JiNan"
<SPAN style="WHITE-SPACE: pre"> </SPAN> };
References to Structure Variables
After defined, it can be referenced.
(1) Refers to the value of a member in a structure variable
Reference method: structure variable name.member name
Where "." is a member operator, which has the highest precedence among all operators.
#include<iostream>
using namespace std;
struct Date{ //声明一个结构体类型Date
int month; //日期中的月份
int day; //日期中的天
int year; //日期中的年份
};
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
Date birthday; //Date是结构体类型,birthday是Date的类型的变量
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
int main(){
Student one={001,"qianshou",'m',19,10,1,1993,100,"JiNan"};
cout<<one.num<<endl;
cout<<one.name<<endl;
cout<<one.sex<<endl;
cout<<one.age<<endl;
cout<<one.birthday.month<<"/"<<one.birthday.day<<"/"<<one.birthday.year<<endl;
cout<<one.score<<endl;
cout<<one.addr<<endl;
return 0;
}
If a member society is also a structure type, several member operators are used to find the lowest-level member level by level.
E.g:
cout<<one.birthday.month<<"/"<<one.birthday.day<<"/"<<one.birthday.year<<endl;
(2) The value of one structure variable can be assigned to another structure variable with the same organization.
#include<iostream>
using namespace std;
struct Date{ //声明一个结构体类型Date
int month; //日期中的月份
int day; //日期中的天
int year; //日期中的年份
};
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
Date birthday; //Date是结构体类型,birthday是Date的类型的变量
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
int main(){
Student two={1,"qianshou",'m',19,10,01,1993,100,"JiNan"};
Student one=two;
cout<<one.num<<endl;
cout<<one.name<<endl;
cout<<one.sex<<endl;
cout<<one.age<<endl;
cout<<one.birthday.month<<"/"<<one.birthday.day<<"/"<<one.birthday.year<<endl;
cout<<one.score<<endl;
cout<<one.addr<<endl;
return 0;
}
(3)可以引用结构体变量的地址,也可以引用结构体变量成员的地址。
#include<iostream>
using namespace std;
struct Date{ //声明一个结构体类型Date
int month; //日期中的月份
int day; //日期中的天
int year; //日期中的年份
};
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
Date birthday; //Date是结构体类型,birthday是Date的类型的变量
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
int main(){
Student two={1,"qianshou",'m',19,10,01,1993,100,"JiNan"};
Student &one=two;
one.num++;
one.birthday.day+=10;
cout<<two.num<<endl;
cout<<two.name<<endl;
cout<<two.sex<<endl;
cout<<two.age<<endl;
cout<<two.birthday.month<<"/"<<two.birthday.day<<"/"<<two.birthday.year<<endl;
cout<<two.score<<endl;
cout<<two.addr<<endl;
return 0;
}
A small example:
#include<iostream>
using namespace std;
struct Date{ //声明一个结构体类型Date
int month; //日期中的月份
int day; //日期中的天
int year; //日期中的年份
};
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex[5]; //声明一个字符型变量sex
int age; //声明一个整形变量age
Date birthday; //Date是结构体类型,birthday是Date的类型的变量
float score; //声明一个单精度型变量
char addr[30]; //声明一个字符型数组addr
};
int main(){
Student one;
//输入信息
cout<<"请输入学号:";
cin>>one.num;
cout<<"请输入姓名:";
cin>>one.name;
cout<<"请输入性别:";
cin>>one.sex;
cout<<"请输入年龄:";
cin>>one.age;
cout<<"请输入生日的年 月 日:";
cin>>one.birthday.year;
cin>>one.birthday.month;
cin>>one.birthday.day;
cout<<"请输入你的成绩:";
cin>>one.score;
cout<<"请输入地址:";
cin>>one.addr;
//输出信息
cout<<"\n以下是你的信息\n";
cout<<"学号:"<<one.num<<endl;
cout<<"姓名:"<<one.name<<endl;
cout<<"性别:"<<one.sex<<endl;
cout<<"年龄:"<<one.age<<endl;
cout<<"生日:"<<one.birthday.year<<"/"<<one.birthday.month<<"/"<<one.birthday.day<<endl;
cout<<"成绩:"<<one.score<<endl;
cout<<"地址:"<<one.addr<<endl;
return 0;
}