欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C的结构体函数

程序员文章站 2022-10-24 16:55:45
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 }