结构体作为参数传入函数错误

结构体作为参数传递的错误! 如下代码:定义了一个结构体变量,将该结构体变量作为参数传递给函数func2,此时传递给函数func2的是结构体变量my_sis的拷贝,函数func2中对所传入的结构体的修改不影响main函数中定义的结构体,所修改的只是传入的拷贝的副本。

1#include <stdio.h>
  2 
  3 struct score
  4 {
  5         int math;
  6         int english;
  7 };
  8 
  9 struct stu
 10 {
 11         int age;
 12         struct score core;
 13 };
 14 
 15 void fun(int b[5])
 16 {
 17         b[3]=8;
 18 }
 19 void fun2(struct stu my)
 20 {
 21         my.core.math = 100;
 22 }
 23 
 24 int main()
 25 {
 26         //int a[5]={1,2,3,4,5};
 27         struct stu my_sis;
 28 
 29         my_sis.core.math = 10;
 30         my_sis.core.english = 50;
 31 
 32         /*
 33         fun(a);
 34         printf("%d
",a[3]);
 35         */
 36         fun2(my_sis);
 37         printf("%d
",my_sis.core.math);
 38 
 39 }


经验分享 程序员 微信小程序 职场和发展