gpt4 book ai didi

c - 从不兼容的指针类型赋值[默认启用]

转载 作者:太空宇宙 更新时间:2023-11-03 23:46:03 25 4
gpt4 key购买 nike

为什么我在 insertNode() 中收到此警告:

warning: assignment from incompatible pointer type [enabled by default]|

在这一行中:

 head->next = newNode; //point head's next to the newNode

warning: initialization from incompatible pointer type [enabled by default]|

在这一行中:

 Node *current = head->next; 

在我的 main() 函数中我也有这个警告:

warning: passing argument 1 of 'insertNode' from incompatible pointer type [enabled by default]|

在这一行中:

insertNode(&head, num);

我的代码中还有许多与这些警告类似的警告。我该如何修复它们?

 typedef struct NodeStruct{
int data;
struct Node *next;
}Node;

void insertNode(Node *head, int data){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;

if(head->next == NULL){
head->next = newNode;
newNode->next = NULL;
}

else{
Node *current = head->next;
while(current != NULL && current->data < data){
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}

int main(int argc, char *argv[])
{
Node *head = malloc(sizeof(NodeStruct));
head->next = null;
insert(head, 22);
insert(head, 55);
insert(head, 44);
insert(head, 2);
insert(head, 2112);
insert(head, 3);


printList(head);
return 0;

}

最佳答案

main 中 -

insertNode(&head, num);
^ don't pass address

它需要 Node * ,你应该这样调用它 -

insertNode(head, num);

其他警告是由于上述错误,因为您传递 head 的地址而不是将 head 传递给 function 。

同样在 struct NodeStruct 中改变这个 -

struct Node *next;         // you cannot reference Node in struct itself

至-

struct NodeStruct *next;   //You need to use structure name 

关于c - 从不兼容的指针类型赋值[默认启用],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33406355/

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