3. 結構數(shù)組和結構指針
結構是一種新的數(shù)據(jù)類型, 同樣可以有結構數(shù)組和結構指針。
一、結構數(shù)組
結構數(shù)組就是具有相同結構類型的變量集合。假如要定義一個班級40個同學
的姓名、性別、年齡和住址, 可以定義成一個結構數(shù)組。如下所示:
struct{
char name[8];
char sex[2];
int age;
char addr[40];
}student[40];
也可定義為:
struct string{
char name[8];
char sex[2];
int age;
char addr[40];
};
struct string student[40];
需要指出的是結構數(shù)組成員的訪問是以數(shù)組元素為結構變量的, 其形式為:
結構數(shù)組元素.成員名
例如:
student[0].name
student[30].age
實際上結構數(shù)組相當于一個二維構造, 第一維是結構數(shù)組元素, 每個元素是
一個結構變量, 第二維是結構成員。
注意:
結構數(shù)組的成員也可以是數(shù)組變量。
例如:
struct a
{
int m[3][5];
float f;
char s[20];
}y[4];
為了訪問結構a中結構變量y[2]的這個變量, 可寫成
y[2].m[1][4]
結構是一種新的數(shù)據(jù)類型, 同樣可以有結構數(shù)組和結構指針。
一、結構數(shù)組
結構數(shù)組就是具有相同結構類型的變量集合。假如要定義一個班級40個同學
的姓名、性別、年齡和住址, 可以定義成一個結構數(shù)組。如下所示:
struct{
char name[8];
char sex[2];
int age;
char addr[40];
}student[40];
也可定義為:
struct string{
char name[8];
char sex[2];
int age;
char addr[40];
};
struct string student[40];
需要指出的是結構數(shù)組成員的訪問是以數(shù)組元素為結構變量的, 其形式為:
結構數(shù)組元素.成員名
例如:
student[0].name
student[30].age
實際上結構數(shù)組相當于一個二維構造, 第一維是結構數(shù)組元素, 每個元素是
一個結構變量, 第二維是結構成員。
注意:
結構數(shù)組的成員也可以是數(shù)組變量。
例如:
struct a
{
int m[3][5];
float f;
char s[20];
}y[4];
為了訪問結構a中結構變量y[2]的這個變量, 可寫成
y[2].m[1][4]

