C 一般无效*声明成员请求';单词';不是一个结构或联盟

C 一般无效*声明成员请求';单词';不是一个结构或联盟,c,struct,void-pointers,C,Struct,Void Pointers,我不熟悉C中的void指针。不幸的是,当我试图调用结构中的变量时,它会抛出错误: [Error] request for member 'word' in something not a structure or union 我的印象是,所有需要做的就是创建一个void*var,然后可以使用它引用任何数据,或者设置为任何值 #include <stdlib.h> struct node{ void* listHeader; struct node *ne

我不熟悉C中的void指针。不幸的是,当我试图调用结构中的变量时,它会抛出错误:

[Error] request for member 'word' in something not a structure or union
我的印象是,所有需要做的就是创建一个void*var,然后可以使用它引用任何数据,或者设置为任何值

    #include <stdlib.h>

struct node{
    void* listHeader; 
    struct node *next; 
}; 

struct otherNode {
   /*The word it stores*/ 
   char* word; 

};

void Method(struct node *header);

int main(){
     struct node *header = malloc(sizeof(struct node)); 
     Method(header); 
     return 0; 
}
void Method(struct node *header){
    (struct otherNode)(header->listHeader).word = "ties";
    
}
#包括
结构节点{
void*列表头;
结构节点*下一步;
}; 
结构其他节点{
/*它存储的单词*/
字符*字;
};
作废方式(结构节点*表头);
int main(){
结构节点*头=malloc(sizeof(结构节点));
方法(标题);
返回0;
}
void方法(结构节点*头){
(struct otherNode)(标题->列表标题).word=“ties”;
}

任何帮助都将不胜感激!!谢谢你抽出时间

这条线有两个问题:

(struct otherNode)(标题->列表标题).word=“ties”

  • 操作员先死<代码>优先于铸造

  • 您正在尝试将(void*)强制转换为(struct otherNode)。你不能这样做,你可以把它转换成指向那个结构的指针,它看起来像:

  • 这将使您的代码得以编译,但不会成功运行,因为您没有将
    头->列表头-/code>设置为指向任何地方(
    头->列表头=malloc(sizeof(struct otherNode));
    ?),所以您正在写入不应该写入的内存区域


    资源:


    这条线有两个问题:

    (struct otherNode)(标题->列表标题).word=“ties”

  • 操作员先死<代码>
  • 优先于铸造

  • 您正在尝试将(void*)强制转换为(struct otherNode)。你不能这样做,你可以把它转换成指向那个结构的指针,它看起来像:

  • 这将使您的代码得以编译,但不会成功运行,因为您没有将
    头->列表头-/code>设置为指向任何地方(
    头->列表头=malloc(sizeof(struct otherNode));
    ?),所以您正在写入不应该写入的内存区域


    资源:

    ((struct otherNode*)header->listHeader)->word = "ties";