You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 capture function's parameters don't connect to your Students array right now—passing individual field pointers won't let you properly store data into the aStudent struct 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 Students struct 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 Students array (struct Students *students)—this lets us modify the original array elements directly (pass-by-reference). We also add a numStudents parameter to make the function flexible.
  • Input Handling: Added getchar() to clear the input buffer after using scanf (since scanf leaves the newline character in the buffer, which would break fgets). We also use strcspn to remove the trailing newline that fgets adds.
  • Display Function: This function takes a single Students struct by value—each time we call it in the loop, we pass a copy of aStudent[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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:00:16