c语言 - 值类型"node"不能用于初始化类型为 "struct node" 的实体



我有两个函数,其中一个调用另一个函数,标题是我得到的错误。我只是想知道我是否缺少一个简单的解决方案。如果需要更多信息,请告诉我。非常感谢。

node createNode() {
newNode temp;//declare node
temp = (newNode)malloc(sizeof(struct node));//allocate memory
temp->next = NULL;//next point to null
return *temp;// return the new node
} 
void enqueue(queue* q, customer* data) {
// Create a new LL node
struct node* temp = createNode(data);//error line

您希望返回值为struct node*,因此返回类型应为struct node*

另外,将指向struct node的指针命名为newNode看起来非常令人困惑(至少对我来说(,所以不应该这样做。

还有一点是malloc()家族的铸造结果被认为是一种不好的做法。

最后,您应该检查malloc()是否成功。

struct node* createNode() { /* use proper return type */
/* use non-confusing type */
struct node* temp;//declare node
temp = malloc(sizeof(struct node));//allocate memory
if (temp == NULL) return temp; /* check if allocation succeeded */
temp->next = NULL;//next point to null
/* remove dereferencing */
return temp;// return the new node
} 
void enqueue(queue* q, customer* data) {
// Create a new LL node
struct node* temp = createNode(data);//error line

此外,参数data被传递但被忽略,这看起来很奇怪,但我不会解决这个问题,因为我不知道如何解决。

最新更新