[英]Incompatible pointer type error using structs
我是編程新手。 我正在努力學習C和指針,但它給了我很多麻煩。 嘗試實現單鏈表時出現以下錯誤。 我在網上搜索,我找不到像我這樣的錯誤的人,或者也許我根本無法理解我的問題。
以下是我收到的錯誤:
警告:不兼容的指針類型初始化'NODE *'(又名'struct node *'),表達式為'struct NODE '[-Wincompatible-pointer-types] NODE temp =(* l) - > head;
NODE* temp = (*l)->head;
在main中,我傳遞了LIST類型變量的地址。 因此,我認為我必須取消引用'l',以獲取LIST類型所在的地址,然后我必須使用箭頭取消引用以獲取NODE所在的地址。 我在哪里困惑? 我非常感謝你的幫助。
您將在下面看到我編寫的代碼:
typedef struct node {
int value;
struct node* next;
}NODE;
typedef struct list{
struct NODE* head;
}LIST;
void insert(LIST** l, int x){
if((*l)->head == NULL){
NODE* new_Node = (NODE*) malloc(sizeof(NODE));
new_Node->next = NULL;
new_Node->value = x;
}
NODE* temp = (*l)->head;
while(temp->next != NULL){
temp=temp->next;
}
NODE* new_Node = (NODE*) malloc (sizeof(NODE));
temp->next = new_Node;
new_Node->next = NULL;
new_Node->value = x;
}
int main(){
LIST *l = (LIST*) malloc(sizeof(LIST));
insert(&l, 5);
return 0;
}
這個:
typedef struct list{
struct NODE* head;
}LIST;
應該是這樣的:
typedef struct list{
NODE* head;
}LIST;
經過測試並編譯好了。
我想你的問題在這里:
typedef struct list
{
struct NODE* head;
}LIST;
只需在NODE
之前刪除struct
關鍵字
typedef struct list
{
NODE* head;
}LIST;
要么
typedef struct list
{
struct node* head;
}LIST;
此外,您需要使用NULL
初始化head
以使此條件成為工作
if((*l)->head == NULL) .....
所以當你創建你的列表時添加l->head = NULL;
LIST *l = malloc(sizeof(LIST));
l->head = NULL;
最后一個(我希望)當你創建你的第一個節點時,你忘記為它分配head
,並返回以便不添加第一個元素兩次
if((*l)->head == NULL)
{
NODE* new_Node = malloc(sizeof(NODE));
new_Node->next = NULL;
new_Node->value = x;
(*l)->head = new_Node;
return;
}
順便說一句, 不要在C中使用malloc
結果
你使用*l
是正確的。 問題在於:
NODE* temp = (*l)->head;
左邊是NODE *
,它與struct node *
相同,但右邊是struct NODE *
。
C區分大小寫, struct node
和struct NODE
是不同的類型。 此外,struct標簽的命名空間與其他類型的命名空間是分開的,因此NODE
和struct NODE
也是不同的類型。
在LIST
的定義中,我認為你的意思是struct NODE* head;
應該是NODE* head;
。 在該行上沒有生成警告,因為在C中通過提及它來隱式聲明結構類型是合法的(即該行也聲明了新類型struct NODE
)。
您的insert
函數中存在代碼重復。 您不應在第二個typedef
語句中使用NODE
之前的struct
關鍵字,因為NODE
已經是類型struct node
的別名。 這就是你收到問題中提到的警告的原因。 另外,你不應該轉換malloc
的結果。 請閱讀本文 - 我是否投出了malloc的結果? 我建議您對代碼進行以下更改。
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *next;
} NODE;
typedef struct list {
NODE *head;
} LIST;
void insert(LIST **l, int x) {
// do not cast the result of malloc.
// also, do not repeat the type on the rhs.
// create the new node to be inserted
NODE *new_Node = malloc(sizeof(*new_Node));
new_Node->next = NULL;
new_Node->value = x;
NODE *temp = (*l)->head;
// check if the head of the list is empty
// if yes, simply assign the new node to head
// and return
if(temp == NULL) {
(*l)->head = new_Node;
return;
}
// reach the last node in the list
while(temp->next != NULL)
temp = temp->next;
// insert the new node to the end of the list
temp->next = new_Node;
}
int main(void) {
LIST *l = malloc(sizeof(*l));
insert(&l, 5);
insert(&l, 10);
// print the value of the head node
printf("%d\n", l->head->value);
// print the value of the next node
printf("%d\n", l->head->next->value);
NODE *head = l->head;
NODE *temp = NULL;
// free the nodes in the list
while(head != NULL) {
temp = head;
head = head->next;
free(temp);
}
// free the pointer to the
// head of the list
free(l);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.