我有一個C程序,用戶在其中輸入一組等級。一切工作正常。 GPA計算正確等。但是,當數字被打印出來時,由於某種原因,學生結構中的兩個指針指向相同的地址,導致兩個學生在打印出信息時顯示第二個等級。其餘信息是正確的,只有等級是相同的。從函數返回數組地址不起作用
我能想到的唯一的事情是第二次初始化成績數組覆蓋第一個。我不明白爲什麼會發生這種情況,或者如何解決這個問題。
以下是該程序的樣品IO:
Enter the number of students:> 2
Enter the number of grades to track:> 3
There are 2 students.
There are 3 grades.
Enter information for student:
Enter SID:> 101
Enter last name:> Enright
Enter first name:> Reed
Enter grades (separated by space):> 70.1 60 92
Enter information for student:
Enter SID:> 123
Enter last name:> Claire
Enter first name:> Heidi
Enter grades (separated by space):> 82.5 96.1 89.0
Student ID #101:
Name: Reed Enright
Grades: 82.5 96.1 89.0
GPA: 74.03
Student ID #123:
Name: Heidi Claire
Grades: 82.5 96.1 89.0
GPA: 89.20
,這是完整的代碼:
#include <stdio.h>
#define NAME_SIZE 25
typedef struct {
int sid;
char last_name[NAME_SIZE];
char first_name[NAME_SIZE];
float *grades;
float gpa;
} Student;
// function prototypes
// get student information
Student prompt_student(int number_of_grades);
// calculate the gpa based on the grades
float calculate_gpa(Student student, int number_of_grades);
// prints all of the students
void print_all_students(Student students[], int number_of_students, int number_of_grades);
int main(){
// initialise variables
int number_of_students;
int number_of_grades;
// prompt for number of students
printf("\nEnter the number of students:> ");
scanf("%d", &number_of_students);
// prompt for number of grades
printf("Enter the number of grades to track:> ");
scanf("%d", &number_of_grades);
// confirm the above
printf("\nThere are %d students. \nThere are %d grades.\n",
number_of_students, number_of_grades);
// initialise student list
Student students[number_of_students];
// get and store student information
for(int i = 0; i < number_of_students; i++){
students[i] = prompt_student(number_of_grades);
}
// confirm the above
print_all_students(students, number_of_students, number_of_grades);
return 0;
}
Student prompt_student(int number_of_grades){
// initialise student variable
Student student;
float grades[number_of_grades];
printf("\nEnter information for student: \n");
// prompt for student info
printf("\tEnter SID:> ");
scanf("%d", &(student.sid));
printf("\tEnter last name:> ");
scanf("%s", student.last_name);
printf("\tEnter first name:> ");
scanf("%s", student.first_name);
printf("\tEnter grades (separated by space):> ");
for(int i = 0; i < number_of_grades; i++){
scanf("%f", &grades[i]);
}
student.grades = grades;
student.gpa = calculate_gpa(student, number_of_grades);
return student;
}
float calculate_gpa(Student student, int number_of_grades){
float total = 0; // initialise variable for sum of grades
// add all grades together
for(int i = 0; i < number_of_grades; i++){
total += student.grades[i];
}
// return average
return total/number_of_grades;
}
void print_all_students(Student students[], int number_of_students, int number_of_grades){
// loop through all students
for(int i = 0; i < number_of_students; i++){
// print student info
printf("\nStudent ID #%d:", students[i].sid);
printf("\n\tName:\t%s %s", students[i].first_name, students[i].last_name);
printf("\n\tGrades:\t");
for(int n = 0; n < number_of_grades; n++){
printf("%.1f ", students[i].grades[n]);
}
printf("\n\tGPA:\t%.2f", students[i].gpa);
}
printf("\n");
}
考慮''student.grades = grades;''prompt_student()''中的行。成績在哪裏? –