[問題] Linked list incompatible pointer type

作者: jeffchen106 (超哥)   2021-09-17 19:53:42
開發平台(Platform): (Ex: Win10, Linux, ...)
Win10
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
https://www.onlinegdb.com/online_c_compiler
問題(Question):
這個code主要是可以動態輸入資料並且創建出Linked list, 並且還能夠排序好.
這段code跑起來沒有問題, 能夠成功. 但不知道為什麼在上面提到的編譯器編譯
時一直出現多行warning: incompatible pointer size. 舉其中兩例如下:
newNodePtr->NextPtr = Headnode;
CurrentPtr = CurrentPtr->NextPtr;
希望能有熱心的高手指點我一下,非常謝謝
錯誤結果(Wrong Output):
擷取部分:
main.c:33:29: warning: assignment from incompatible pointer type
[-Wincompatible-pointer-types]
main.c:40:20: warning: assignment from incompatible pointer type
[-Wincompatible-pointer-types]
main.c:44:29: warning: assignment from incompatible pointer type
[-Wincompatible-pointer-types]
main.c:45:30: warning: assignment from incompatible pointer type
[-Wincompatible-pointer-types]
main.c:50:29: warning: assignment from incompatible pointer type
[-Wincompatible-pointer-types]
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
// Online C compiler to run C program online
#include <stdio.h>
#include
<stdlib.h>
typedef struct node
{
int data;
int *NextPtr;
}Node;
Node *Headnode;
void insert_node(int element)
{
Node *PreviousPtr;
Node *CurrentPtr;
Node *newNodePtr = malloc(sizeof(Node));
Node *tempNode;
if(newNodePtr == NULL)
{
printf("Not enough space!");
return;
}
newNodePtr->data = element;
newNodePtr->NextPtr = NULL;
CurrentPtr = Headnode;
if(Headnode == NULL)
{
Headnode = newNodePtr;
return;
}
if(element < Headnode->data)
{
newNodePtr->NextPtr = Headnode;
Headnode = newNodePtr;
return;
}
while((element > (CurrentPtr->data)) && ((CurrentPtr->NextPtr) != NULL))
{
PreviousPtr = CurrentPtr;
CurrentPtr = CurrentPtr->NextPtr;
}
if(CurrentPtr->data > element)
{
newNodePtr->NextPtr = CurrentPtr;
PreviousPtr->NextPtr = newNodePtr;
}
else
{
newNodePtr->NextPtr = CurrentPtr->NextPtr;
CurrentPtr->NextPtr = newNodePtr;
}
return;
}
void Print_node()
{
Node *Currentnode;
Currentnode = Headnode;
printf(" %d
作者: j0958322080 (Tidus)   2021-09-17 20:29:00
看起來是你的 next 型別那邊有問題
作者: gusion   2021-09-17 20:29:00
你的NextPtr是指到int,應該要指到struct node才對
作者: jeffchen106 (超哥)   2021-09-18 07:16:00
對不起不是很懂>< 像第33行報warning的那邊,NextPtr指到的是Headnode,那應該也算是指到struct node啊?
作者: pponywong (pony)   2021-09-18 08:54:00
int* NextPtr; 改成 struct node* NextPtr;
作者: sarafciel (Cattuz)   2021-09-18 08:55:00
你把月餅塞進巧克力蛋糕的紙盒拿去送人 就定義上來說
作者: pponywong (pony)   2021-09-18 08:55:00
這個用法是 struct 裡面宣告 struct pointer type
作者: sarafciel (Cattuz)   2021-09-18 08:56:00
你還是送了月餅沒錯 但人家會疑惑怎麼用巧克力蛋糕的外盒也是很合理的呀
作者: wawi2 (@@)   2021-09-18 09:33:00
用dummy head可以解決你code裡的一堆NULL check
作者: jeffchen106 (超哥)   2021-09-18 15:31:00
感謝各位!我放進月餅盒之後果然warning解除了!

Links booklink

Contact Us: admin [ a t ] ucptt.com