指向结构体变量的指针的应用
#include <stdio.h> #include <string.h> 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 <stdio.h> #include <string.h> 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; }
时间: 2024-10-29 16:36:41