[英]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);
}
}
當我嘗試執行此代碼時,出現以下錯誤:
incompatible types when assigning to type ‘struct stnode *’ from type ‘StudentNode’
代碼有什么問題,我已經嘗試了很多東西,但是沒有用。 我只想參考下一個我正在分析的“學生”
讀取編譯器錯誤 (您尚未執行此代碼-尚未編譯...)
類型'StudentNode'中的'struct stnode *'
基本上,您嘗試將結構分配給指針,但這不起作用。 查看以下行:
(**(sections+students[i].section)).next=*(students+i);
問題在於(students + 1)
的取消引用 。
問題是*(students+i)
正在取消引用元素students+i
。 它應該是:
(**(sections+students[i].section)).next=students+i;
更改此:
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);
}
}
對此:
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.