嗨,朋友们,我正在练习结构。我有这两个函数,一个返回结构,然后将其复制到 main 中的本地结构。我的第二个函数通过输入不同的实体来更改那些本地结构成员。现在我在调用每个函数后打印了结果,令我惊讶的是,我注意到两个函数之后的打印结果是相同的。我无法理解这里发生了什么……你们能解释一下吗……谢谢!
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[30];
float marks;
};
struct student read_student();
void read_student_p(struct student student3);
void print_student(struct student student2);
int main()
{
struct student student1;
student1 = read_student();
print_student(student1);
read_student_p(student1);
print_student(student1);
system("pause");
return 0;
}
//This is my first function
struct student read_student()
{
struct student student2;
printf("enter student name for first function: \n");
scanf("%s",&student2.name);
printf("enter student marks for first function:\n");
scanf("%f",&student2.marks);
return student2;
}
//function to print
void print_student(struct student my_student)
{
printf("Student name in first function is : %s\n",my_student.name);
printf("Student marks in first function are: %f\n",my_student.marks);
};
//My second function
void read_student_p(struct student student3)
{
printf("enter student name for second function: \n");
scanf("%s",&student3.name);
printf("enter student marks for second function: \n");
scanf("%f",&student3.marks);
}