首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不兼容类型-- struct*和struct,

不兼容类型-- struct*和struct,
EN

Stack Overflow用户
提问于 2014-09-07 07:07:51
回答 2查看 4.8K关注 0票数 2

谢谢你抽出时间阅读我的问题,我看了几个类似的问题,这些问题在这种情况下似乎没有帮助,尽管它们可能会帮助其他有类似麻烦的人:

C: Incompatible types?

Struct as incompatible pointer type in C

Incompatible Types Error with Struct C

我试图在c (-std=c99)中创建一个简单的链接列表结构,我的结构在这一点上是相当通用的:

代码语言:javascript
复制
typedef struct
{
  int count;
  char* word;
  struct node *nextNode;
}node;

然后在一个函数中,我有一个“根”或"head“节点:

代码语言:javascript
复制
node *root;
root = (node *) malloc(sizeof(node));

我尝试将一个node分配给后面函数中的根节点nextNode,如下所示:

代码语言:javascript
复制
if(root->nextNode == 0)
{
  root->nextNode = foo;
}

这会导致错误:

代码语言:javascript
复制
 "error incompatibles types when assigning to type `struct node*` from type `node`

&foo没有改善这种情况,反而导致了lvalue required as unary样式的错误。

以下是围绕我的问题的背景:

代码语言:javascript
复制
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>

    typedef struct
    {
        int count;
        char* word;
        struct node *nextNode;
    }node;

    node makenode(char *word)
    {
        node x;
        x.word = word;
        x.count = 1;
        return x;
    }

    void processInput(int threshold, const char* filename)
    {   
        node *root;
        root = (node *) malloc(sizeof(node)); 
        root->nextNode = 0;  
        char* word;
        while(fgets(word, 29, stdin) != NULL){
            if(root->nextNode == 0)
            {
                root->nextNode = makenode(word);
            }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-07 07:23:06

问题

代码语言:javascript
复制
    typedef struct             // make an alias for a structure with no tag name
    {
        int count;
        char* word;
        struct node *nextNode; // with a pointer to struct node (which does not exit)
    }node;                     // name the alias node

解决方案

代码语言:javascript
复制
    typedef struct node        // make an alias for a structure with tag name node
    {
        int count;
        char* word;
        struct node *nextNode; // with a pointer to struct node (which is this one)
    }node;                     // name the alias node
票数 2
EN

Stack Overflow用户

发布于 2020-10-27 18:57:05

试试这段代码

代码语言:javascript
复制
     #include <stdio.h>
    #include <malloc.h>
    #include <string.h>

    typedef struct node //should give a name for this 
    {
        int count;
        char* word;
        struct node *nextNode;
    }node;

    static node *makenode(char *word) //start with static and returning type is node* because we are storing this output in root->nextNode which is *node pointer
    {
        node x;
        x.word = word;
        x.count = 1;
        return x;
    }

    void processInput(int threshold, const char* filename)
    {   
        node *root;
        root = (node *) malloc(sizeof(node)); 
        root->nextNode = NULL;  //pointer values should initialised to NULL not 0
        char* word;
        while(fgets(word, 29, stdin) != NULL){
            if(root->nextNode == NULL) ////pointer values should initialised to NULL not 0
            {
                root->nextNode = makenode(word);
            }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25707934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档