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

C - Passing Structure Parameters to Function By Reference and Value

我能解释一下我在做什么吗wrong.The程序是用于输入和显示学生详细信息的。它必须使用一个结构并具有两个功能;一个用于捕获(通过引用传递),另一个用于显示(通过值传递。

这是我的代码:

#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.");
    } 

}

我已经测试了很多次并且它工作正常,但我收到以下错误消息: Errors are shown for every argumety I've used

此外,当我完成其中一种情况时,有没有一种方法或一行代码可以用来 return 切换到开关的开始 - A "Return to Main Menu" ?

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

首先,当您调用两个函数 capture() 和 display() 时,您尝试传递(通过值或通过引用)的变量,没有' t 在任何地方使用,因为当您捕获或显示结果时,您直接与 结构 Students 的成员打交道。

你得到 语法错误的原因是因为 capture() 需要变量的地址 (&name,&age,&address,&course) 而你正在传递变量 (name,age,地址,课程)自己。而且你也在使用,

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

而不是

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

在我看来,与其将 Structure 数组设为全局,不如在 main 函数中声明它并将整个结构数组作为引用传递给 capture() 并将其按值传递给 display() 对您的 objective 因为您需要在代码中同时使用按值调用和引用调用。

我稍微编辑了您的代码并将 return 添加到主菜单选项 。它对我有用,如果我的回答很长且难以理解,我深表歉意,因为这是我在 Whosebug 中的第一个答案。谢谢!

#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.");
        }

    }

}