【问题标题】:Incompatible types struct* and struct,不兼容的类型 struct* 和 struct,
【发布时间】:2014-09-07 07:07:51
【问题描述】:

感谢您花时间阅读我的问题, 我看过一些类似的问题,在这种情况下它们似乎没有帮助 虽然可以帮助其他有类似问题的人:

C: Incompatible types?
Struct as incompatible pointer type in C
Incompatible Types Error with Struct C

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

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

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

node *root;
root = (node *) malloc(sizeof(node));

我稍后在函数中尝试将node 分配给根节点nextNode,如下所示:

if(root->nextNode == 0)
{
  root->nextNode = foo;
}

这会导致错误:
“从类型 node 分配给类型 struct node* 时出现错误不兼容类型
&foo 不会改善这种情况,而是导致 lvalue required as unary 样式错误。

这是围绕我的问题的背景:

    #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);
            }

【问题讨论】:

  • 错误信息似乎很清楚。 x*(“指向 x 的指针”)和 x 是不同的类型。
  • 你当然是正确的,但是我不确定如何解决这个问题,尽管它看起来很简单。
  • 变量 'foo' 是如何定义的。 root->nextNode 的类型是指向定义的“结构节点”的指针。什么是 foo 类型? foo 是否定义为“node *foo”?您应该显示包含 foo 定义的代码部分。
  • 节点富; foo.word = word_val; foo.count = 1;
  • 您不能将node 分配给node *。发布更多与foo 的构造相关的代码(直接在问题中,而不是在 cmets 中)。您几乎肯定需要动态构造foo(通过malloc)才能使其工作。

标签: c pointers struct incompatibletypeerror


【解决方案1】:

问题

    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

解决方案

    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

【讨论】:

  • 这可能会给我带来问题,但在这一点上没有帮助。
  • 应用此功能并使用 *root->nextNode 后,它似乎运行良好。我对为什么需要 * 并不完全满意,但它确实有效。谢谢。
  • @user16249:您问题中的最新代码现在暴露了更多错误,并且与您在此评论中所说的不符。您能否更新您的问题以反映您所做的最新更改,以便我们解释剩余的问题?
【解决方案2】:

试试这个代码

     #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);
            }
}

【讨论】:

    猜你喜欢
    • 2021-01-17
    • 2021-02-10
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    相关资源
    最近更新 更多