例11.3指向结构体变量的指针的应用。
#include <string.h>
main()
{struct student
{long num;
char name[20];
char sex;
float score;
};
struct student stu-1;
struct student * p;
p=&stu-1;
stu-1.num=89101;
strcpy(stu-1.name,"Li Lin");
stu-1.sex='M';
stu-1.score=89.5;
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",stu-1.num,stu-1.name,stu-1.sex,stu-1.score);
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
}
程序运行结果如下:
No.:89101
name:Li Lin
sex:M
score:89.500000
No:89101
name:Li Lin
sex:M
score:89.500000
#include <string.h>
main()
{struct student
{long num;
char name[20];
char sex;
float score;
};
struct student stu-1;
struct student * p;
p=&stu-1;
stu-1.num=89101;
strcpy(stu-1.name,"Li Lin");
stu-1.sex='M';
stu-1.score=89.5;
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",stu-1.num,stu-1.name,stu-1.sex,stu-1.score);
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
}
程序运行结果如下:
No.:89101
name:Li Lin
sex:M
score:89.500000
No:89101
name:Li Lin
sex:M
score:89.500000