【问题标题】:How to fix: initialization from incompatible pointer type如何修复:从不兼容的指针类型初始化
【发布时间】:2021-03-09 05:27:27
【问题描述】:

当我编译并运行程序时,它说:在函数'addToTheEnd'中:[警告]从不兼容的指针类型初始化//它指向这一行->knot_t *current = start;

如何解决?我是C新手,所以我不明白应该改变什么。我试图理解它,但我无法理解。我的目标是在我运行这个程序时得到一些输出,但没有任何显示。

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

typedef struct knot {
    int number;
    struct knot *next;
} knot_t;

int addToTheEnd(knot_t **start, int value) {
    knot_t *new_ = (knot_t *)malloc(sizeof(knot_t));
    if (new_ == NULL) {
        return 1;
    }
    new_ -> number = value;
    new_ -> next = NULL;
   
    if (start == NULL) {
        *start = new_;
    } else {
        knot_t *current = start;
        while (current -> next != NULL) {
            current = current -> next;
        }
        current -> next = new_;
    }
   
    return 0;
}

void printList(knot_t *start) {
    knot_t *current = start;
    while (current != NULL) {
        printf("%d ", current->number);
        current = current -> next;
    }
}

void clearList(knot_t *start) {
    knot_t *current = start;
    while (current != NULL) {
        knot_t* trenutni = current;
        current = current -> next;
        free(trenutni);
    }
}

int main() {
    knot_t *start = NULL;
    int i = 0;
    for (i = 0; i < 10; i++) {
        if(addToTheEnd(&start, i)) {
        printf("Fail\n");
return EXIT_FAILURE;
}
    }
   
    printList(start);
    clearList(start);
   
    return EXIT_SUCCESS;
}

【问题讨论】:

  • start 的类型为 knot **current 的类型为 knot *。它们的指针类型不兼容,这意味着你做错了。
  • if (start == NULL) { --> if (*start == NULL) { 你想检查传递的指针是否是NULL,但是你检查的是本地指针(这总是给你true
  • 请注意[Warning] initialization from incompatible pointer type 实际上几乎总是错误而不是警告。
  • 它可能应该是knot_t *current = *start;,因为start 是指向knot_t 的指针,它来自main() 中的addToTheEnd(&amp;start, i),您需要取消引用它才能到达mainstart 变量的值。
  • 你也解决了if (start == NULL) 的问题吗?

标签: c struct linked-list singly-linked-list implicit-conversion


【解决方案1】:

函数有knot_t **类型的参数start

int addToTheEnd(knot_t **start, int value) {

而局部变量current 的类型为knot_t *

knot_t *current = start;

因此,由于没有从 knot_t ** 类型到 knot_t * 类型的隐式转换,编译器会发出错误。

看来你的意思

knot_t *current = *start;

也是if语句的条件

 if (start == NULL) {
        *start = new_;

必须

 if (*start == NULL) {
        *start = new_;

另外,如果函数在成功的情况下返回 1 而不是 0,逻辑上会更加一致。

函数可以通过以下方式定义

int addToTheEnd( knot_t **start, int value ) 
{
    knot_t *new_knot = malloc( sizeof( knot_t ) );
    int success = new_knot != NULL;

    if ( success )
    {
        new_knot -> number = value;
        new_knot -> next = NULL;
   
        while ( *start != NULL ) start = &( *start )->next;

        *start = new_knot;
    } 
   
    return success;
}

由于函数printList 不会改变指向的节点,它至少应该被声明为

void printList( const knot_t *start) {
    const knot_t *current = start;
    while (current != NULL) {
        printf("%d ", current->number);
        current = current -> next;
    }

【讨论】:

  • 是的,来自莫斯科的@Vlad,就是这样。谢谢你的详细解答! Спасибо Владимир :)
【解决方案2】:

这对我有用:

int addToTheEnd(knot_t** start, int value) {
  knot_t* new_ = (knot_t*)malloc(sizeof(knot_t));
  if (new_ == NULL) {
    return 1;
  }
  new_->number = value;
  new_->next = NULL;

  if (*start == NULL) {    // start -> *start
    *start = new_;
  }
  else {
    knot_t* current = *start;    // start -> *start
    while (current->next != NULL) {
      current = current->next;
    }
    current->next = new_;
  }

  return 0;
}

【讨论】:

  • 是的,它有效,其他人也是这么说的。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多