可用指针来指向结构类型数组元素,设pStudent为指向结构类型数组元素的指针变量,则pStudent也指向该结构类型数组元素,则pStudent也指向该结构类型数组的1个元素,pStudent+1指向2号元素,pStudent+i则指向i+1号元素,这与普通数组的情况是一样的
实例
#include <stdio.h>
#include <stdlib.h>
#define N 3
int main(void)
{
struct StudentType
{
int num;
char *name;
char sex[3];
int age;
float score;
};
struct StudentType student[N]=
{
{10101,"µÂ»ªÁõ","ÄÐ",42,98.1},
{10102,"ö¦Ó°ÕÅ","Å®",22,99.8},
{10103,"ѧÓÑÕÅ","ÄÐ",43,96.5}
};
struct StudentType *pStudent;
for(pStudent=student;pStudent<student+N;pStudent++){
printf("%8d",pStudent->num);
printf("%12s",pStudent->name);
printf("%6s",pStudent->sex);
printf("%6d",pStudent->age);
printf("%8.1f\n",pStudent->score);
}
system("PAUSE");
return 0;
}