贺老师教学链接C语言及程序设计进阶本课讲解
指向结构体变量的指针的应用
#include
#include
struct Student
{
int num;
char name[12];
char sex;
float score;
};
int main( )
{
struct Student stu;
stu.num=10301;
strcpy(stu.name, "Wang Fun");
stu.sex='f';
stu.score=89.5;
struct Student *p=&stu;
printf("%d %s %c %.1f\n", stu.num, stu.name, stu.sex, stu.score);
printf("%d %s %c %.1f\n", (*p).num, (*p).name, (*p).sex, (*p).score);
printf("%d %s %c %.1f\n", p->num, p->name, p->sex, p->score);
return 0;
}
用指向结构体变量的指针作实参
#include
#include
struct Student
{
int num;
char name[12];
float score[3];
};
void print(struct Student*);
int main( )
{
struct Student stu,*pt;
stu.num=12345;
strcpy(stu.name, "Li Fung");
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
pt=&stu;
print(pt);
printf("%d %s ", stu.num, stu.name);
printf("%.1f %.1f %.1f\n", stu.score[0], stu.score[1], stu.score[2]);
return 0;
}
void print(struct Student *p)
{
printf("%d %s ", p->num, p->name);
printf("%.1f %.1f %.1f\n", p->score[0], p->score[1], p->score[2]);
p->score[2]=100;
}
如果觉得《c语言程序设计指针进阶 C语言及程序设计进阶例程-15 指向结构体的指针》对你有帮助,请点赞、收藏,并留下你的观点哦!