C - 传递结构体参数给函数,通过引用和值

3

请问我做错了什么,能否给我一些解释?这个程序是用来输入和显示学生信息的。它必须使用结构体并且有两个函数;一个是捕获(需要通过引用传递),另一个是显示(需要通过值传递)。

以下是我的代码:

#include <stdio.h>

struct Students{
    int ID;
    char name[50];
    int age;
    char address[100];
    char course[30];
} aStudent[5];

void capture(char *name , int *age , char *address, char *course){
        int i;

        for(i=0; i<5; i++){
        aStudent[i].ID = i+1;

        printf("\nFor Student number %d:\n",aStudent[i].ID);

        printf("Enter Student Name: ");
        scanf ("%s", &aStudent[i].name);

        printf("Enter Student Age: ");
        scanf ("%d", &aStudent[i].age);

        printf("Enter Student Address: ");
        scanf ("%s", &aStudent[i].address);

        printf("Enter Course: ");
        scanf ("%s", &aStudent[i].course);
    }
}

void display(char name, int age , char address, char course){
    int i;

        for(i=0; i<5; i++){
          printf("\nStudent %d:\n",aStudent[i].ID);
           printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse: 
 %s",aStudent[i].name, aStudent[i].age, aStudent[i].address, 
aStudent[i].course);

        printf("\n");
    }
}

void main()
{
    int option, age;
    char  name, address, course;

    printf("\t...Welcome to the Student Data System...\n\n");

    printf("\nPlease Select An Option: \n1. Input Student Data\n2. View 
   Student Data\n3. Exit Syatem\n\n");
    scanf("%d",&option);


    switch(option){
        case 1:
            printf("Enter Student Details:\n");
             capture(name, age , address, course);
            break;
        case 2:
            printf("\nDisplaying Information:\n");
            display(name, age , address, course);
            break;
        case 3:
            close();
            break;
        default:
            printf("\nSorry, your option is not valid.");
    } 

}

我已经测试了多次,它可以正常工作,但是我收到了以下错误消息:每个使用的参数都显示错误 此外,是否有一种方法或一行代码可以在完成一个case后返回switch的开头 - 即“返回主菜单”?

对于你的最后一个问题,你可以使用循环(例如 while(true) {...}),在其中读取用户的输入。 - mnille
2
为什么在 displaycapture 中传递参数却没有使用它们?另外,close 是什么?而且在 C 语言中没有 "按引用传递" 的概念。所有参数都是按值传递的。 - Jabberwocky
在您的程序中,不需要将参数传递给函数。 - Karthick
2
C语言不支持传递引用(pass-by-reference),而是严格的传值调用(pass-by-value)。指针不是引用! - too honest for this site
抱歉,我的意思是调用,而不是传递。 - Dante
2个回答

2
首先,在调用两个函数capture()和display()时,你试图通过值或引用传递的变量都没有被使用,因为在捕获或显示结果时,你直接处理结构体Students的成员。
而你得到语法错误的原因是因为capture()期望变量的地址(&name,&age,&address,&course),而你却传递了变量本身(name,age,address,course)。此外,你还使用了:
scanf ("%s", &aStudent[i].name);

替代

scanf ("%s", aStudent[i].name);

在我看来,与其将Structure数组设为全局变量,不如在main函数中声明它,并将整个结构数组作为引用传递给capture()函数,同时将其按值传递给display()函数,这样更符合你的目标,因为你需要在代码中同时使用按值和按引用调用。
我已经对你的代码进行了一些编辑,并添加了返回主菜单选项。这对我有效,如果我的回答过长或难以理解,请谅解,因为这是我在stackoverflow上的第一个回答。谢谢!
#include <stdio.h>

struct Students
{
    int ID;
    char name[50];
    int age;
    char address[100];
    char course[30];
};

void capture(struct Students *aStudent)
{
    int i;

    for(i=0; i<2; i++)
    {
        aStudent[i].ID = i+1;

        printf("\nFor Student number %d:\n",aStudent[i].ID);

        printf("Enter Student Name: ");
        scanf ("%s", aStudent[i].name);

        printf("Enter Student Age: ");
        scanf ("%d", &aStudent[i].age);

        printf("Enter Student Address: ");
        scanf ("%s", aStudent[i].address);

        printf("Enter Course: ");
        scanf ("%s", aStudent[i].course);
    }
}

void display(struct Students aStudent[])
{
    int i;

    for(i=0; i<2; i++)
    {
        printf("\nStudent %d:\n",aStudent[i].ID);
        printf("Name: %s\t\tAge: %d\t\tAddress: %s\t\tCourse: %s",aStudent[i].name, aStudent[i].age, aStudent[i].address,aStudent[i].course);

        printf("\n");
    }
}

void main()
{
    struct Students aStudent[2];
    int option;
    char choice = 'Y';

    printf("\t...Welcome to the Student Data System...\n\n");

    while(choice == 'Y' || choice == 'y')
    {
        printf("\nPlease Select An Option: \n1. Input Student Data\n2. View Student Data\n3. Exit Syatem\n\n");
        scanf("%d",&option);
        switch(option)
        {
        case 1:
            printf("Enter Student Details:\n");
            capture(aStudent);
            printf("Return to main menu? (Y/N) :");
            scanf(" %c",&choice);
            break;
        case 2:
            printf("\nDisplaying Information:\n");
            display(aStudent);
            printf("Return to main menu? (Y/N) :");
            scanf(" %c",&choice);
            break;
        case 3:
            close();
            break;
        default:
            printf("\nSorry, your option is not valid.");
        }

    }

}

但我不太明白你在这里的意思是什么:“在我看来,与其将结构数组设为全局变量,不如在主函数内声明它,并将整个结构数组作为引用传递给capture(),并将其按值传递给display(),因为你需要在代码中同时使用按值和引用传递。” 不需要道歉。 - Dante
通过在全局范围内声明**aStudent [5],您的函数capture()display()都可以访问数组中的数据,而不需要通过按值或引用传递参数。您说您想通过引用调用将数据传递给capture()和通过按值调用将数据传递给display()。因此,最好是在main函数中声明aStudent [5]**并通过参数将其传递给函数,就像我在上面的代码中所做的那样。 - Nipun Sampath

-1
你没有初始化字符串,而是初始化了字符!char name, address, course; 如果你使用了'char *name, *address, *course;'或者char name[100], address[100], course[100]; 你可能已经得到了答案!如果你使用了上述情况,你应该使用scanf("%s",name);来扫描!希望我回答了你的问题!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接