您好朋友我在練習結構。我有這兩個函數返回結構,我將它複製到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);
}
會發生什麼,你期望發生什麼? – Patashu 2013-03-21 03:04:38
即使在調用第二個函數後,它也會打印相同的名稱和標記......您可以看到,在第二個函數中,我們再次輸入新名稱和新標記....我期望它能夠打印 – studyembedded 2013-03-21 03:07:36