1

I have a code like this for a student management system. The input function works fine but I haven't figured out why my output function stop immediately when i call it.( I know that i can not return a local array from a function in C, but i assign the array to a pointer and return that pointer, is it ok?)

Here is my code:

 struct Student
{

    char name[50];
    char birth[25];
    char gender[10];
    float math, physics;

};

struct Student* input(int n, struct Student *p)
{
    int i, id = 1;

    struct Student s[n];

    getchar();

    for(i = 0; i < n; i++)
    {


        printf("Name: ");
        fgets(s[i].name, 50, stdin);
        s[i].name[strlen(s[i].name)-1] = '\0';

        printf("Date of birth: ");
        fgets(s[i].birth,25,stdin);
        s[i].birth[strlen(s[i].birth)-1] = '\0';

        printf("Gender: ");
        fgets(s[i].gender,10,stdin);
        s[i].gender[strlen(s[i].gender)-1] = '\0';

        printf("Math = ");
        scanf("%f", &s[i].math);

        printf("Physics = ");
        scanf("%f", &s[i].physics);

        getchar();
    }
    p = s;
return p;
}


void outPut(int n, struct Student *p)
{   
    int i;
    for(i = 0; i < n; i++)
    {
        printf("%s %s %s %f %f\n",  p[i].name, p[i].birth, p[i].gender, p[i].math, p[i].physics);
    }
}


int main()
{
    int n;

    struct Student *p, *p1;
    int choice;

    printf("-----------Student Management-----------");
    printf("\n1. Add new students.\n2. Print student list.\n");

    do
    {
        printf("Your choice = ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Number of students = ");
                scanf("%d", &n);
                input(n,p);
                break;

            case 2:
                outPut(n,p);
                break;

        }
    }
    while(choice!=0);
return 0;
}
Thuan Nguyen
  • 431
  • 6
  • 18
  • 1
    *but i assign the array to a pointer and return that pointer, is it ok?* -- no. – Ajay Brahmakshatriya Jun 27 '18 at 04:04
  • The problem is `struct Student *p, *p1;` declare two **uninitialized pointers** that do not point to any valid storage. You need to allocate storage with `malloc` or `calloc` or `realloc`. (you also need to validate the **return** of `scanf` every time -- or you are just asking for trouble. – David C. Rankin Jun 27 '18 at 04:07
  • 2
    Playing with pointers does not change the fact that the array is a local variable of the `input` function and will therefore be destroyed when that function returns – M.M Jun 27 '18 at 04:14
  • `struct Student s[n]; ... p = s; return p;` attempts to returns the address of local `struct Student s[n];`. That [train have left the station](https://www.idiomsandslang.com/train-left-station/). [It is gone](http://www.azquotes.com/quote/646063). It is _undefined behavior_ (UB). Don't do that. – chux - Reinstate Monica Jun 27 '18 at 04:26
  • Related or even a duplicate to https://stackoverflow.com/q/47028165/694576 – alk Jun 27 '18 at 05:01

1 Answers1

1

You are defining your array as a local variable. It means that it will no longer exist after the function ends. To avoid this, declare your array as a pointer and use malloc to initialize it:

struct Student *s = malloc(sizeof(Student) * n);

It will behave as a regular array and you will be able to use itself as the function return.

Hugo Sartori
  • 560
  • 6
  • 21