简体   繁体   中英

C - Passing Structure Parameters to Function By Reference and Value

Could I please have an explanation of what I am doing wrong.The program is for inputting and displaying student details. It must use a structure and have 2 functions; one to capture(which is to be passed by reference) and another to display (which is to be passed by value.

Here is my code:

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

}

I have tested a number of times and it's working, but I'm getting these error messages: Errors are shown for every argumety I've used

Also, is there a way or a line(s) of code i can use to return to the start of the switch when I am done with one of the cases - A "Return to Main Menu"?

First of all, variables you have tried to pass (either by value or by reference) when you are calling the two functions capture() and display() , haven't used anywhere because you are dealing directly with the members of the structure Students when you are capturing or displaying the results.

And the reasons you are getting syntax errors are because the capture() is expecting the addresses of the variables (&name,&age,&address,&course) and you are passing variables (name,age,address,course) themselves. And also you are using,

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

instead of

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

In my opinion instead of making the Structure array global, declaring it inside main function and passing the whole structure array to the capture() as a reference and passing it by value to the display() is better to your objective as you need to use both call by value and reference in your code.

I have edited your code a bit and added the return to main menu option . It works for me and I apologize if my answer is long and hard to understand because this is my first answer in stackoverflow. Thanks!

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

    }

}

You have not initialized your string instead you have initialized a character! char name, address, course; If you have used 'char *name, *address, *course;' or char name[100], address[100], course[100]; you might have got the answer ! If you use the above case you should scan using scanf("%s",name); ! Hope I answered your question !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM