结构体变量作函数参数就可以了,比如,在下面的程序中,在main()函数中定义了结构体变量stu_1,然后输入学生学号,姓名,调用自定义函数change(),把stu_1作为函数参数。change()的形参stu_2,也是一个结构体变量,就会由stu_1整体赋值,即对应的成员赋值。然后进行学号,姓名的修改。最后输出。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int number;
char name[10];
};
void change(struct student stu_2);
int main()
{
struct student stu_1;
printf("please input student 1 number:\n");
scanf("%d",&stu_1.number);
printf("please input student 1 name:\n");
scanf("%s",stu_1.name);
change(stu_1);
return 0;
}
void change(struct student stu_2)
{
printf("\nbefore change:\n");
printf("student 2 number is:%d\n",stu_2.number);
printf("student 2 name is:%s\n",stu_2.name);
strcpy(stu_2.name,"jim");
stu_2.number=1009;
printf("after change:\n");
printf("student 2 number is:%d\n",stu_2.number);
printf("student 2 name is:%s\n",stu_2.name);
}
运行结果如下,如果有什么不明白的还可以问我