指针变量输出结构体数组元素
/*用指针变量输出结构体数组元素。*/
#include <stdio.h> //定义一个结构体并同时进行初始化 struct student{ int num; char *name; char sex; int age; } stu[5] = { {20100290,"lihua",'F',18}, {20100291,"liuxing",'M',19}, {20100292,"huangke",'F',19}, {20100295,"fengshou",'F',19}, {20100296,"Wangming",'M',18} }; int main(int argc, const char * argv[]) { int i; //定义一个指向结构体的指针 struct student *ps; printf("学号\t\t姓名\t\t性别\t年龄\t\n"); /*用指针变量输出结构体数组元素。*/ for(ps=stu;ps<stu+5;ps++) printf("%d\t%-10s\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age); //华丽的分割线 printf("---------------------------------\n"); /*用数组下标法输出结构体数组元素学号和年龄。*/ for(i=0;i<5;i++) printf("%d\t%-10s\t%c\t%d\t\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age); return 0; }