//通过指针变量访问并输出结构数组的各元素
#include<iostream.h>
#include<conio.h>
#define student__num 5
struct student
{
int num;
char name[20];
char sex;
int score;
};
void displaystudentinfo(const student *const);
int main()
{
student theclass[student__num]={
{110,"wei shanshan",'M',98},
{120,"bai yushuang",'N',78},
{130,"liu linlinff",'N',89.5},
{140,"xiao haizi",'M',78.5},
{150,"cao jingjing",'M',69.7},
};
student *pstud=theclass; //pstud 为指向student 结构的指针,其值被初始化成数组theclass的首地址,
for(int i=0;i<student__num;i++)//通过时pstud自增,使得pstud指向theclass[i],然后调用函数输出数组中各元素的成员值
displaystudentinfo(pstud++);
getch();return 0;
}
void displaystudentinfo(const student *const stud)//const 限定防止原数组被改变
{
cout<<"num="<<stud->num<<"\t";
cout<<"name="<<stud->name<<"\t";
cout<<"sex="<<stud->sex<<"\t";
cout<<"score="<<stud->score<<endl<<endl;
}