首先结构体数组的定义:
struct Student
{
int id;
float weight,high;
}stu1[10],stu2[10][20];//一维结构体数组stu1,和二维结构体数组stu2.
完整代码:
#include<stdio.h>
struct date
{
int year,month,day;
}; //不要忘记分号
struct Student
{
int id;
char name[10];
char sex[2];
struct date birthday;
}; //分号不能少哦
int main()
{
int i;
printf("id\t name\t sex\t year\t month\t day\n");
struct Student s[2]={
{20221000,"xiaoming","M",2004,9,30},
{20221001,"xiaohong","W",2004,12,20}
};//定义了结构体数组s,并对其进行初始化
for(i=0;i<2;i++)
{
printf("%d\t %s\t %s\t %d\t %d\t %d\n",s[i].id,s[i].name,s[i].sex,
s[i].birthday.year,s[i].birthday.month,s[i].birthday.day);
}//引用结构体变量s的数据成员birthday中的成员year,month,day
return 0;
}
对于结构体中其他结构体类型的数据成员的引用: