【问题标题】:C - Not working (node) structures in main function (intentionally avoiding use of singly linked list implementation)C - 主函数中的不工作(节点)结构(故意避免使用单链表实现)
【发布时间】:2018-08-02 17:42:01
【问题描述】:

我正在尝试创建一个没有实现的单链表。我这样做只是为了更深入地了解结构的工作原理。我只想在主函数中创建 2 个节点并将它们链接起来。我可以在结构声明中使用 typedef 并通过实现来做到这一点,但这不是我想要的(我可以成功地做到这一点)。

我写了一些代码,但是在第 27 行出现错误(同样的问题出现在第 29、30 和 33 行)。我知道这个问题的解释很简单,但我在我的脑海或网络(或书籍)上都找不到它。我将不胜感激在这个问题上的任何帮助。去年我一直在编程。

我想在 CodeReview 中寻求帮助,但根据他们的常见问题解答,这不是寻求此类问题帮助的地方。

错误是:

27:11:错误:从类型“struct NODO”分配给类型“struct NODO”时类型不兼容

E.sig = F;

其他行也有同样的问题。一切都在同一个文件 (.c) 中。

这是完整的代码:

#include <stdio.h>
#include <stdlib.h>

struct NODE {
    int data;
    struct NODE *next;
};

struct LIST {
    struct NODE *first;
    struct NODE *last;
    int size;
};


void main(){

    struct NODE E;
    struct NODE F;
    struct LIST L;

    E.data = 1;
    E.next = NULL;

    F.data = 2;
    F.next = NULL;
    E.next = F;          // line 27

    L.first = E;         // line 29
    L.last = F;          // line 30

    struct NODE *ptr;
    ptr = E;             // line 33

    while(ptr != NULL){
        printf("%i\n", ptr->data);
        ptr = ptr->next;
    }
}

【问题讨论】:

    标签: c data-structures linked-list main


    【解决方案1】:

    问题是您试图将对象分配给指针。您应该分配节点的地址并记下节点对象本身。

    试试E.next = &amp;F;,其他的都一样。

    【讨论】:

    • 而且我认为您给我们的错误消息错过了类似&amp; 字符的内容。你删除了吗?
    • 错误消息中更可能缺少*
    • 我复制的消息中没有缺少操作数。我仔细检查了一遍。但是,显然带有 * 的错误消息是在最后一个错误消息中完成的,而不是在前两个错误消息中。它说:30:11: error: incompatible types when assigning to type 'struct NODE *' from type 'struct NODE'
    • 在 C 中提到结构时说“对象”是否正确? @BenjaminBarrois 你写了“试图将一个对象分配给一个指针”。
    • @CarlosW.Mercado 结构只是创建对象(或实例,如果您愿意)的模式。您不能混淆结构声明和实现(模式)struct X { ... }; 及其实例化X myX; X myOtherX;。这里我们讨论的是结构的绑定实例,而不是结构本身。
    【解决方案2】:

    指针是一个变量,其值为另一个变量的地址,即内存位置的直接地址。所以你必须提供内存位置的地址。在C中,变量的地址是使用&operator导出的。

    修改后的代码:-

    #include <stdio.h>
    #include <stdlib.h>
    
    struct NODE {
        int data;
        struct NODE *next;
    };
    
    struct LIST {
        struct NODE *first;
        struct NODE *last;
        int size;
    };
    
    
    int main(){
    
        struct NODE E;
        struct NODE F;
        struct LIST L;
    
        E.data = 1;
        E.next = NULL;
    
        F.data = 2;
        F.next = NULL;
        E.next = &F;          // not  E.next = F; 
    
        L.first = &E;         // not  L.first = E;
        L.last = &F;          // not  L.last = F;
    
        struct NODE *ptr;
        ptr = &E;             // not ptr = E;   
    
        while(ptr != NULL){
            printf("%i\n", ptr->data);
            ptr = ptr->next;
        }
    
        return 0;
    }
    

    建议使用int main() 而不是void main()

    输出:-

    1
    2
    

    【讨论】:

    • 为什么建议在main函数中更改返回值类型?
    【解决方案3】:

    在将问题发送给 SO 几分钟后,我找到了解决方案或至少一些其他信息,这些信息使我找到了解决方案。

    在 Wikipedia (https://en.wikipedia.org/wiki/Struct_(C_programming_language)#Pointers_to_struct) 中,我找到了下一个 sn-p 代码:

    struct point {
       int x;
       int y;
    };
    struct point my_point = { 3, 7 };
    struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                     and initialize it with the address of my_point. */
    

    我的错误是使用指针指向结构而不是结构的地址。指针只能指向地址(它们存储其值),它们不能指向对象。

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2021-08-22
      • 2017-09-13
      • 2016-11-26
      • 2011-08-16
      相关资源
      最近更新 更多