例3:指向结构体数组的指针变量的使用
structdata/*定义结构体类型*/
{
intday,month,year;
};
structstu/*定义结构体类型*/
{
char name[20];
long num;
struct data birthday;
};
main()
{inti;
structstu*p,student[4]={{"liying",1,1978,5,23},{"wangping",2,1979,3,14},
{"libo",3,1980,5,6},{"xuyan",4,1980,4,21}};
/*定义结构体数组并初始化*/
p=student;/*将数组的首地址赋值给指针p,p指向了一维数组student*/
printf("/n1----Outputname,number,year,month,day/n");
for(i=0;i<4;i++)/*采用指针法输出数组元素的各成员*/
printf("%20s%10ld%10d//%d//%d/n",(p+i)->name,(p+i)->num,
(p+i)->birthday.year,(p+i)->birthday.month,
(p+i)->birthday.day);
}
|