看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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 -->", Currentnode->data); while(Currentnode->NextPtr !=NULL) { Currentnode = Currentnode->NextPtr; printf(" %d -->", Currentnode->data); } if(Currentnode->NextPtr == NULL) printf("NULL"); } int main() { int choice = 0; int element; Headnode = NULL; int del_result; printf("Enter your choice:\n"); printf("1 to insert an element into the list\n"); printf("2 to delete an element from the list\n"); printf("3 to end"); while(choice != 3) { printf("\n?"); scanf("\n%d", &choice); switch(choice) { case 1: printf("Enter a integer: "); scanf("%d", &element); insert_node(element); Print_node(); break; case 3: printf("End of Run"); break; } } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.71.109.75 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1631879624.A.1E2.html ※ 編輯: jeffchen106 (219.71.109.75 臺灣), 09/17/2021 19:54:14
j0958322080: 看起來是你的 next 型別那邊有問題 09/17 20:29
gusion: 你的NextPtr是指到int,應該要指到struct node才對 09/17 20:29
jeffchen106: 對不起不是很懂>< 像第33行報warning的那邊,NextPtr 09/18 07:16
jeffchen106: 指到的是Headnode,那應該也算是指到struct node啊? 09/18 07:16
pponywong: int* NextPtr; 改成 struct node* NextPtr; 09/18 08:54
sarafciel: 你把月餅塞進巧克力蛋糕的紙盒拿去送人 就定義上來說 09/18 08:55
pponywong: 這個用法是 struct 裡面宣告 struct pointer type 09/18 08:55
sarafciel: 你還是送了月餅沒錯 但人家會疑惑怎麼用巧克力蛋糕的外 09/18 08:56
sarafciel: 盒也是很合理的呀 09/18 08:57
wawi2: 用dummy head可以解決你code裡的一堆NULL check 09/18 09:33
jeffchen106: 感謝各位!我放進月餅盒之後果然warning解除了! 09/18 15:31