简体   繁体   中英

Assigning same value to 2d struct object array

Struct student
{
        char last_name[30];
        char first_name[30];
};
Struct examination_seating
{
        struct student** seating;
};
void student_init_default(struct student *p)
{
        *p->first_name=*"###";
        *p->last_name=*"###";
}
void examination_seating_init(int rowNum, int columnNum, struct examination_seating *t)
{
        for (int i=0; i<rownNum; i++)
        {
                for(int j=0; j<columnNum; j++)
                {
                        student_init_default(&t->seating[i][j]);
//this creates a read access violation
                }
        }
}

I am working on a school project and have it written but am having difficulty debugging. My TA and teacher is providing little help. I submitted parts of the code I am having issues with. I need to assign a default value from student_init_default function to the array. If I try to do it with char I get errors as well. I couldn't find any clear references online.

        *p->first_name=*"###";
        *p->last_name=*"###";

C strings cannot be assigned this way (this assigns only the first character). We have to use strcpy :

        strcpy(p->first_name, "###");
        strcpy(p-> last_name, "###");

Moreover, since student_init_default(&t->seating[i][j]) creates a read access violation , you haven't correctly initialized t or t->seating[i] , so it was unwise that you only submitted parts of the code you're having issues with .

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