[英]C error with structs and pointers
typedef struct stnode {
unsigned number;
char * name;
unsigned section;
struct stnode * next;
} StudentNode;
void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students) {
if(!num_students) return ;
StudentNode * aux=NULL;
for(int i=0;i<num_students;i++){
aux=sections[students[i].section];
(**(sections+students[i].section)).next=*(students+i);
}
}
When I try do execute this code I have this error: 当我尝试执行此代码时,出现以下错误:
incompatible types when assigning to type ‘struct stnode *’ from type ‘StudentNode’
What is the problem with the code, I already tried lots of things, but non have worked. 代码有什么问题,我已经尝试了很多东西,但是没有用。 I just want to refer the next to the "student" that i'm analyzing
我只想参考下一个我正在分析的“学生”
Read the compiler error (you are not executing this code yet - it hasn't compiled...) 读取编译器错误 (您尚未执行此代码-尚未编译...)
'struct stnode *' from type 'StudentNode'
类型'StudentNode'中的'struct stnode *'
Basically you are trying to assign a structure to a pointer, this doesn't work. 基本上,您尝试将结构分配给指针,但这不起作用。 Look at the following line:
查看以下行:
(**(sections+students[i].section)).next=*(students+i);
The problem lies in the de-reference of (students + 1)
. 问题在于
(students + 1)
的取消引用 。
The problem is that *(students+i)
is de-referencing the element students+i
. 问题是
*(students+i)
正在取消引用元素students+i
。 It should be: 它应该是:
(**(sections+students[i].section)).next=students+i;
Change this: 更改此:
void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students) {
if(!num_students) return ;
StudentNode * aux=NULL;
for(int i=0;i<num_students;i++){
aux=sections[students[i].section]; // = *(x) you are dereferencing students+1
(**(sections+students[i].section)).next=*(students+i);
}
}
To this: 对此:
void buildStudentSections(StudentNode * sections[], StudentNode students[], size_t num_students)
{
if(!num_students) return ;
StudentNode * aux=NULL;
for(int i=0;i<num_students;i++)
{
aux=sections[students[i].section]; //removed '*`
(**(sections+students[i].section)).next = (students+1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.