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

前端 未结 3 871
离开以前
离开以前 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 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.

提交回复
热议问题