使用结构不兼容的指针类型错误

时间:2014-04-29 05:27:51

标签: c pointers struct

我是编程新手。我正在努力学习C和指针,但它给了我很多麻烦。 尝试实现单链表时出现以下错误。我在网上搜索过,我找不到像我这样的错误的人,或者我的问题可能无法理解它。

以下是我收到的错误:

警告:不兼容的指针类型初始化' NODE *'       (又名' struct node *'),其表达式为' struct NODE '       [-Wincompatible指针类型]         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;
}

4 个答案:

答案 0 :(得分:3)

此:

typedef struct list{
    struct NODE* head;
}LIST;

应该是这样的:

typedef struct list{
    NODE* head;
}LIST;

通过测试进行测试和编译。

答案 1 :(得分:3)

我想你的问题就在这里:

typedef struct list
{
     struct NODE* head;
}LIST;

只需在struct

之前删除NODE关键字
typedef struct list
{
     NODE* head;
}LIST;

typedef struct list
{
     struct node* head;
}LIST; 

此外,您需要使用head初始化NULL以使此条件成为工作

  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;
      }

而BTW,don't cast malloc results in C

答案 2 :(得分:1)

您对*l的使用是正确的。问题在于:

NODE* temp = (*l)->head;

左侧是NODE *,与struct node *相同,但右侧是struct NODE *

C区分大小写,struct nodestruct NODE是不同类型。此外,struct标签的命名空间与其他类型的命名空间是分开的,因此NODEstruct NODE也是不同的类型。

我认为,在LIST的定义中,您的意思是struct NODE* head;应为NODE* head;。在该行上没有生成警告,因为在C中通过提及它来隐式声明结构类型是合法的(即此行也声明了新类型struct NODE)。

答案 3 :(得分:0)

insert功能中存在代码重复。您不应在第二个struct语句中使用NODE之前的typedef关键字,因为NODE已经是struct node类型的别名。这就是你收到问题中提到的警告的原因。此外,您不应该投射malloc的结果。请阅读此内容 - Do I cast the result of 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;
}