[英]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.
我需要从 student_init_default 函数中为数组分配一个默认值。 If I try to do it with char I get errors as well.
如果我尝试用 char 来做,我也会出错。 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). C 字符串不能以这种方式分配(这仅分配第一个字符)。 We have to use
strcpy
:我们必须使用
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 .此外,由于
student_init_default(&t->seating[i][j])
创建了一个读访问冲突,你没有正确初始化t
或t->seating[i]
,所以你只提交了部分代码是不明智的你有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.