C - initialize array of structs

前端 未结 4 715
情歌与酒
情歌与酒 2020-12-14 18:49

I am having a problem initializing an array of structs. I\'m not sure if I am doing it right because I get \"initialization from incompatible pointer type\" & \"assignme

4条回答
  •  渐次进展
    2020-12-14 19:54

    Unrelated to the compiler warnings, but your initial malloc is wrong; you want:

    malloc(sizeof(student *)* numStudents)
    

    To allocate room for a total of 'numStudents' pointers to a student. The line:

    students[x] = (struct student*)malloc(sizeof(student));
    

    Should be:

    students[x] = (student*)malloc(sizeof(student));
    

    There's no such thing as 'struct student'. You've declared an unnamed struct and typedef'd it to 'student'. Compare and contrast with:

    struct student
    {
        char* firstName;
        char* lastName;
        int day;
        int month;
        int year;
    
    };
    

    Which would create a 'struct student' type but require you (in C) to explicitly refer to struct student rather than merely student elsewhere. This rule is changed for C++, so your compiler may be a bit fuzzy about it.

    As for:

    student* newStudent = {"john", "smith", 1, 12, 1983};
    

    That should be:

    student newStudent = {"john", "smith", 1, 12, 1983};
    

    As the curly brace syntax is a direct literal, not something somewhere else that you need to point to.

    EDIT: on reflection, I think aaa may have taken more of an overview of this than I have. Is it possible that you're inadvertently using an extra level of pointer dereference everywhere? So you'd want:

    student* students = malloc(sizeof(student) * numStudents);
    
    /* no need for this stuff: */
    /*int x;
    for(x = 0; x < numStudents; x++)
    {
        //here I get: "assignment from incompatible pointer type" 
        students[x] = (struct student*)malloc(sizeof(student));
    }*/
    
    int arrayIndex = 0;
    

    And:

    student newStudent = {"john", "smith", 1, 12, 1983};
    
    //add it to the array
    students[arrayIndex] = newStudent;
    arrayIndex++;
    

    Subject the array not being used outside of scope of newStudent. Otherwise copying the pointers to strings is incorrect.

提交回复
热议问题