C的结构体函数

 1 #include<stdio.h>
 2 #include<string.h>
 3 struct Test
 4 {
 5     int age;
 6     char name[16];
 7     double score;
 8 }std1;
 9 //结构体函数
10 struct Test struct_fun(int age,char *name,double score){
11     struct Test Student;
12     Student.age=age;
13     //Student->name=name; 错误,字符串不能直接赋值,可以初始化
14     //example :char NAME[16]="hello";
15     strcpy(Student.name,name);
16     Student.score=score;
17 
18     return Student;
19 }
20 void output_fun1(int age,char *name,double score){
21    std1.age=age;
22    strcpy(std1.name,name);
23    std1.score=score;
24    printf("output_fun1 :\n");
25    printf("age=%d,name=%s,score=%.2f\n",std1.age,std1.name,std1.score);
26 }
27 //结构体函数输出
28 void output_fun2(struct Test stu){
29      printf("output_fun2 :\n");
30      printf("age=%d,name=%s,score=%.2f",stu.age,stu.name,stu.score);
31 }
32 
33 void main(){
34     output_fun1(15,"xiaoming",89);
35     
36     struct Test p=struct_fun(17,"xiaohong",80);
37     output_fun2(p);
38 }

 

posted @ 2019-06-23 15:59  胡子拉碴叔  阅读(6895)  评论(0编辑  收藏  举报