33.对学生结构体的数据进行修改

来源:互联网 发布:校园网络借贷主题班会 编辑:程序博客网 时间:2024/05/20 06:08

程序通过定义学生结构体变量,存储了学号、姓名、和3门课的成绩,函数fun的功能是讲形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。


#include<stdio.h>#include<string.h>struct student{long sno;char name[10];float score[3];};struct student fun(struct student a){int i;a.sno = 1002;strcpy_s(a.name,10, "LiSi");for (i = 0;i < 3;i++)a.score[i]+= 1;return a;}int main(){struct student s = { 1001,"ZhangSan",95,80,88 }, t;int i;printf("\n\nThe original data:\n");printf("\nNo:%ld   Name:%s\nScore: ", s.sno, s.name);for (i = 0;i < 3;i++)printf("%6.2f", s.score[i]);printf("\n");t = fun(s);printf("\nThe data after modified:\n");printf("\nNo:%ld  Name :%s\n  Score: ", t.sno, t.name);for (i = 0;i < 3;i++)printf("%6.2f", t.score[i]);printf("\n");getchar();return 0;}


0 0