Array of structs in c: giving all the strings same values (with the int it works well). What sould I do?

前端 未结 3 872
离开以前
离开以前 2021-01-21 10:09

When I run the program and give values to the id, name, surname it gives them all the value of the last student. For instance if the last students name is Anna then all the othe

相关标签:
3条回答
  • 2021-01-21 10:49

    The student structure only holds pointers to char arrays. Now the actual space in memory for the strings is allocated in your for cycle an its lifetime is limited by the scope of the for cycle, accessing it afterwards is undefined behaviour. In your case the space where the strings are has not been reused and overridden yet, so coincidentally you are able to get the last value stored (cases like this can also raise segmentation fault).

    You should have a look on some basic tutorial about pointers and c memory model. Allocating the arrays in the struct is a good solution (nucleon's answer). Also the scanf function can overflow you should limit the number of retrieved characters to match the size of allocated array.

    0 讨论(0)
  • 2021-01-21 11:00

    You should reserve memory for the string attributes. As a hint, use a struct similar to:

    #define MAX_LEN 32
    
    struct Student{     /*struct for student info*/
        char id[MAX_LEN];
        char name[MAX_LEN];
        char surname[MAX_LEN];
        int grade;
    };
    

    Define MAX_LEN to something that is reasonable for you, and check that the entered values aren't any longer. Also make sure to strcpy the input values to the struct variables.

    0 讨论(0)
  • 2021-01-21 11:01

    The issue is that the loop variables go out of scope after each iteration, and you're left with dangling pointers in the Student instances. What you're seeing is the result of undefined behavior.

    What's probably happening is that the same char array is being passed into every student instance. Then you modify the same char array, overwriting the previous values.

    You'll need to make copies of the strings. Remember to create a function like Student_free where you free the dynamically allocated copies.

    struct Student* Student_new(char* id, char* name, char* surname, int grade) 
    { 
    
        struct Student* st = malloc(sizeof(struct Student));
        st->id = strndup(id, 10);
        st->name = strndup(name, 10);
        st->surname = strndup(surname, 10);
        st->grade = grade;
        return st;
    }
    
    0 讨论(0)
提交回复
热议问题