【问题标题】:incompatible pointer type in c pointersc指针中不兼容的指针类型
【发布时间】:2013-06-06 07:18:01
【问题描述】:
#include<stdio.h>
#include<stdlib.h>


struct node {
    int data;
    struct node *next;
};

int insert (struct node *head, int data);
int print (struct node *head);

int main()
{
    struct node *head;

    head = NULL;
    // printf("%d\n",head);
    insert(&head,5);
    insert(&head,4);
    insert(&head,6);
    print(&head);
    print(&head);
    print(&head);
}

int insert(struct node *head,int data) {
    if(head == NULL) {
        head = malloc(sizeof(struct node));
        head->next = NULL;
        head->data = data;
        // printf("%d\n",data);
    }
    else {
        struct node *tmp = head;
        if(tmp->next!=NULL) {
            tmp = tmp->next;
        }

        tmp->next  = malloc(sizeof(struct node));
        tmp->next->next = NULL;
        tmp->next->data = data;
        // printf("%d\n",data);
    }
}

int print (struct node *head) {
    printf("hello entered here\n");
    struct node *tmp = head;

    if (head == NULL) {
        printf("entered null\n");
        return;
    }

    while (tmp != NULL) {
        if (tmp->next == NULL) {
            printf("%0d", tmp->data);
        } else {
            printf("%0d -> ", tmp->data);
        }
        tmp = tmp->next;
    }
    printf("\n");
}

我在编译时收到以下警告

In function main:
insert.c:16: warning: passing argument 1 of insert from incompatible pointer type
insert.c:17: warning: passing argument 1 of insert from incompatible pointer type
insert.c:18: warning: passing argument 1 of insert from incompatible pointer type
insert.c:19: warning: passing argument 1 of print from incompatible pointer type
insert.c:20: warning: passing argument 1 of print from incompatible pointer type
insert.c:21: warning: passing argument 1 of print from incompatible pointer type

当我运行时,我会得到以下输出

hello entered here
0 -> 5 -> 6
hello entered here
0 -> 5 -> 6
hello entered here
0 -> 5 -> 6

请帮我删除此警告。
你还可以帮我添加一个函数来删除 C 中的节点
我做错了什么?
我应该将**head 传递给函数吗?

【问题讨论】:

  • 你试过 print(head);代替?
  • printf("%d",head) in main 打印一个 0

标签: c pointers linked-list


【解决方案1】:

当前函数 print() 和 insert() 期望 struct node* 而您传递 struct node **。如果您想传递一个副本,请将 &amp; 放在代码中的函数调用中。

如果要修改指针head,传递指针指针并相应修改参数:

  #include<stdio.h>
  #include<stdlib.h>


    struct node {
      int data;
      struct node *next;
    };
    int insert (struct node **head, int data);
    int  print  (struct node **head);

    int main()
    {
     struct node *head;
     head = NULL;
    // printf("%d\n",head);
     insert(&head,5);
     insert(&head,4);
     insert(&head,6);
     print(&head);
     print(&head);
     print(&head);

    } 
    int  insert(struct node **head,int data){

      if(*head == NULL){
        *head = malloc(sizeof(struct node));
        (*head)->next = NULL;
        (*head)->data = data;
    //    printf("%d\n",data);
      }
    else {
      struct node *tmp = *head;
      if(tmp->next!=NULL){
        tmp = tmp->next;
      }
    tmp->next  = malloc(sizeof(struct node));
      tmp->next->next = NULL;
      tmp->next->data = data;
    //  printf("%d\n",data);
    }

    }


    int print (struct node **head) {
      printf("hello entered here\n");
        struct node *tmp = *head;
        if (*head == NULL) {
          printf("entered null\n");
            return;
        }
        while (tmp != NULL) {
            if (tmp->next == NULL) {
                printf("%0d", tmp->data);
            } else {
                printf("%0d -> ", tmp->data);
            }
            tmp = tmp->next;
        }
        printf("\n");
    }

【讨论】:

  • 我只想传递 struct node *head 任何我不想用 **head 运行它的地方
  • 嘿,*head 和 (*head) 有什么区别。
  • 你真的很想使用**head,因为你似乎想修改指针head-&gt; 绑定比* 更紧密,所以*head-&gt;next*(head-&gt;next) 而你想使用(*head)-&gt;next。这就是为什么必须有括号的原因。
  • 它并不清楚 *head->next 和 (*head)->next 之间的区别。能详细解释一下吗
  • @kingsindian 根据operator precedence 不是*a-&gt;b(*a)-&gt;b 相同?
【解决方案2】:

您的insert() 气味 - 太复杂了。这实际上是某种 OO。

这是我的方式,直接输入:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// class node_t
typedef struct __node_s *node_t; // Mind the pointer here.
struct __node_s {
    int data;
    node_t next;
};
node_t node_init(void); // Constructor.
void node_append(node_t, int);
void node_drop_last(node_t);
void node_print(node_t);
void node_fini(node_t); // Destructor.
// end class node_t

int main(void)
{
    node_t head = node_init();
    node_append(head, 5);
    node_append(head, 4);
    node_append(head, 6);
    node_print(head);
    node_drop_last(head);
    node_print(head);
    node_fini(head);
    head = NULL;
    return 0;
}

node_t node_init(void)
{
     node_t node = malloc(sizeof(struct __node_s));
     assert(node);
     memset(node, 0, sizeof(struct __node_s));
     return node;
}

void node_insert(node_t head, int data)
{
    node_t last = head, new = node_init();
    for (; last->next; last = last->next);
    new->data = data;
    last->next = new;
}

void node_drop_last(node_t head)
{
    node_t last = head;
    if (!head->next)
        return;
    for (; last->next->next; last - last->next);
    node_fini(last->next);
    last->next = NULL;
}

void node_print(node_t head)
{
    for (node_t this = head->next; this; this = this->next)
    {
        printf("%d", this->data);
        if (this->next)
            putchar(' '); // A lot faster!
    }
    putchar('\n');
}

void node_fini(node_t head)
{
    if (head->next)
    {
        node_fini(head->next);
        head->next = NULL;
    }
    free(head);
}

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 2013-11-06
    • 2019-08-09
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多