C语言:结构体参数按值与按引用传递问题排查(学生信息程序)
Fixing Your Student Information Program
Hey there! Let's work through your student info program issue. I see your code got cut off mid-for loop, but based on your requirements (using structs, a pass-by-reference capture function, and a pass-by-value display function), I can spot some key issues and share a corrected, complete version of your code.
First, let's break down the problems in your partial code:
- Your
capturefunction's parameters don't connect to yourStudentsarray right now—passing individual field pointers won't let you properly store data into theaStudentstruct array. - You weren't leveraging the struct itself for the capture function; instead of passing separate pointers, you should pass a pointer to the struct array (this is proper pass-by-reference for your use case).
- The display function needs to accept a single
Studentsstruct by value, then you'll loop through your array and pass each element to it.
Here's the corrected full code:
#include <stdio.h> #include <string.h> // Define the Students struct struct Students { int ID; char name[50]; int age; char address[100]; char course[30]; } aStudent[5]; // Array to hold 5 students // Capture function: pass-by-reference using struct pointer void capture(struct Students *students, int numStudents) { for (int i = 0; i < numStudents; i++) { printf("\nEnter details for Student %d:\n", i + 1); printf("ID: "); scanf("%d", &students[i].ID); // Clear input buffer before reading strings getchar(); printf("Name: "); fgets(students[i].name, sizeof(students[i].name), stdin); // Remove newline from fgets output students[i].name[strcspn(students[i].name, "\n")] = '\0'; printf("Age: "); scanf("%d", &students[i].age); getchar(); printf("Address: "); fgets(students[i].address, sizeof(students[i].address), stdin); students[i].address[strcspn(students[i].address, "\n")] = '\0'; printf("Course: "); fgets(students[i].course, sizeof(students[i].course), stdin); students[i].course[strcspn(students[i].course, "\n")] = '\0'; } } // Display function: pass-by-value (takes a single struct) void display(struct Students student) { printf("\n--- Student Details ---\n"); printf("ID: %d\n", student.ID); printf("Name: %s\n", student.name); printf("Age: %d\n", student.age); printf("Address: %s\n", student.address); printf("Course: %s\n", student.course); } int main() { // Capture 5 students' info capture(aStudent, 5); // Display each student's info by passing each struct by value for (int i = 0; i < 5; i++) { display(aStudent[i]); } return 0; }
Key fixes and explanations:
- Capture Function: We now pass a pointer to the
Studentsarray (struct Students *students)—this lets us modify the original array elements directly (pass-by-reference). We also add anumStudentsparameter to make the function flexible. - Input Handling: Added
getchar()to clear the input buffer after usingscanf(sincescanfleaves the newline character in the buffer, which would breakfgets). We also usestrcspnto remove the trailing newline thatfgetsadds. - Display Function: This function takes a single
Studentsstruct by value—each time we call it in the loop, we pass a copy ofaStudent[i], which matches your requirement of pass-by-value. - Main Function: Ties it all together: first captures the student data, then loops through the array to display each entry.
内容的提问来源于stack exchange,提问作者Dante
相关产品推荐
相关产品推荐

