一、定義:
結構體(struct)是由一系列具有相同類型或不同類型的數據構成的數據集合,也叫結構。
聲明一個結構體類型的形式是:
structstudent //聲明一個結構體類型student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
intage; //聲明一個整形變量age
floatscore; //聲明一個單精度浮點型變量score
}
二、結構體類型變量的定義方法及初始化
1、定義結構體變量的方法:
(1)先聲明結構體類型再定義變量名
#include <iostream>
using namespace std;
int main()
{
struct Student //聲明一個結構體類型student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
intage; //聲明一個整形變量age
floatscore; //聲明一個單精度浮點型變量score
}; //要記得這兒的這個分號一定要有,否則會顯示student1 和student2 undeclared identifier
Student student1,student2; //定義結構體變量類型student1和student2
cout<<sizeof(Student)<<endl;
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
運行結果:36
36
36
Press any key to continue
注:在定義了結構體變量之後,系統會爲之分配內存單元。(可以用sizeof函數查看分配的字節數,不同的編譯系統存在差異,本文中的所有運行結果都是在VC6.0下的)
(2)在聲明類型的同時定義變量
#include<iostream>
usingnamespace std;
int main()
{
structStudent //聲明一個結構體類型student
{
int num; //聲明一個整形變量num
char name[20]; //聲明一個字符型數組name
char sex; //聲明一個字符型變量sex
int age; //聲明一個整形變量age
float score; //聲明一個單精度浮點型變量score
}student1,student2; //定義結構體變量類型student1和student2
cout<<sizeof(Student)<<endl;
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
運行結果:如(1)
(3)直接定義結構體類型變量
#include <iostream>
using namespace std;
int main()
{
struct
{
int num; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
int age; //聲明一個整形變量age
float score;//聲明一個單精度浮點型變量score
}student1,student2;//定義結構體變量類型student1和student2
cout<<sizeof(student1)<<endl;
cout<<sizeof(student2)<<endl;
return 0;
}
運行結果36
36
Press any key to continue
注:這種定義方法雖然合法,但是不常用。比較常用的是第一種方法。
關於結構體的類型要注意的幾點:
(1)類型與變量是不同的概念,不要混淆。只能對結構體變量中的成員賦值,而不能對結構體類型賦值。
(2)對結構體變量中的成員(即“域”),可以單獨使用,它的作用與地位相當於同類型的普通變量。
(3)結構體的成員也可以是一個結構體變量。
#include <iostream>
using namespace std;
structDate //聲明一個結構體變量Date
{
intyear; //日期中的年份
intmonth; //日期中的月份
intday; //日期中的天
};//注意此處的分號必須是在英文字符的輸入狀態下的!!
struct Student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
intage; //聲明一個整形變量age
floatscore; //聲明一個單精度浮點型變量score
}; //注意此處的分號一定要在英文字符的輸入狀態下輸入的!!
int main()
{
Studentchengd;
Dateriqi;
cout<<sizeof(chengd)<<endl;
cout<<sizeof(riqi)<<endl;
return 0;
}
運行結果:36
12
Press any key to continue
(5)結構體中的成員名可以與程序中的變量名相同,但二者沒有關係。
例如,程序中可以另定義一個整形變量,他與student中的num是兩回事,互不影響。
2 結構體變量的初始化
(1)在定義結構體時對結構體變量指定初始值
#include <iostream>
using namespace std;
struct Student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
intage; //聲明一個整形變量age
floatscore; //聲明一個單精度浮點型變量score
}student={10000,"chengd","man",19,"100"};
(2)在定義變量時進行初始化(這種方法更常用)
#include <iostream>
using namespace std;
struct Student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
intage; //聲明一個整形變量age
floatscore; //聲明一個單精度浮點型變量score
};
Studentstudent={10000,"chengd","man",19,"100"};
3、結構體變量的引用
在定義了結構體變量之後,就可以引用這個變量。
(1)引用結構體變量中的一個成員的值
引用方式:結構體變量名.成員名
其中“.”是成員運算符,它在所有的運算符中優先級最高。
#include <iostream>
using namespace std;
struct Date
{
intyear;
intmonth;
intday;
};
struct Student
{
intnum; //聲明一個整形變量num
charname[20]; //聲明一個字符型數組name
charsex; //聲明一個字符型變量sex
Datebirthday; //Date 是結構體類型,birthday是Date的類型的變量
floatscore; //聲明一個單精度浮點型變量score
};
intmain()
{
StudentA={001,"chengd",'m',1994,11,11,100};
cout<<A.num<<endl;
cout<<A.name<<endl;
cout<<A.sex<<endl;
cout<<A.birthday.year<<'/'<<A.birthday.month<<'/'<<A.birthday.day<<endl;
cout<<A.score<<endl;
return0;
}運行結果:1
chengd
m
1994/11/11
100
如果一個成員本身也是一個結構體類型,則要用若干個成員運算符,一級一級地找到最低一級的成員。
例如:cout<<A.birthday.month<<"/"<<A.birthday.day<<"/"<<A.birthday.year<<endl;
(2)可以將一個結構體變量的值付給另外一個具有形同機構的結構體變量。
#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(){
Studenttwo={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;
}
運行結果:
2
Qianshou
M
19’10/11/1993
100
Jinan
一個小實例:
#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;
}