gpt4 book ai didi

c - 在结构内部使用时的指针相等性

转载 作者:太空宇宙 更新时间:2023-11-04 04:00:26 24 4
gpt4 key购买 nike

很长一段时间以来,我一直试图让一个指针等于另一个指针,但它就是做不到,我也不确定为什么。

结构是:

typedef struct{
struct listNode* next;
} listNode;

typedef struct{
listNode* head;
} linkedList;

但是在代码中,当我尝试执行时:节点->下一个=列表->头

我收到“来自不兼容指针类型的分配”错误。任何帮助将不胜感激,因为我真的可以看到任何错误!谢谢

最佳答案

你在上面的 typedef 中提到了一个 struct listNode,但它没有声明。

您需要按正确的顺序做事:

               +----At this point, we can refer to "struct listNode"
v
struct listNode {
struct listNode *next; /* We know this self-references, uses the same name. */
};

/* Now establish a shorthand name for the struct ... */
typedef struct listNode listNode;

/* ... which we can use in subsequent declarations like this. */
typedef struct {
listNode *head;
} linkedList;

关于c - 在结构内部使用时的指针相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12402665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com