#include<stdio.h> int main() { struct student { int number; char name[20]; char sex; int age; char addr[30]; }; struct stu1{ int num; char name; struct student; }py[2]; struct student s[3] = { {10000,"Zhang",'m',21,"Shang Hai"}, {10001,"Li",'f',20,"Bei Jing"}, {10002,"Liu",'m',22,"Guang Dong"} }; struct student *p; //定义结构体指针,其指向结构体的首地址,需要注意的是其没有成员,故不可以直接引用student里面的成员,这是我经常发生的错误,切忌
printf("number%d\n",p->addr);//此处错误,还未获得成员 for(p=s; p<s+3; p++) printf("%d,%s,%c,%d,%s\n",p->number,p->name,p->sex,p->age,p->addr);
printf("number_add%d\n",p->addr) ;//正确,已经指向了定义了的结构体首部,可以引用成员了! return 0; }
|