C 如何修复:从不兼容的指针类型初始化

C 如何修复:从不兼容的指针类型初始化,c,struct,linked-list,singly-linked-list,implicit-conversion,C,Struct,Linked List,Singly Linked List,Implicit Conversion,当我编译并运行该程序时,它说:在函数“addToTheEnd”中:[警告]从不兼容的指针类型初始化//并且它指向这一行->knot\u t*current=start 如何修复它?我是C语言的新手,所以我不知道应该改变什么。我试着去理解它,但我无法理解。我的目标是在运行该程序时从中获得一些输出,但什么也不显示 #include <stdio.h> #include <stdlib.h> typedef struct knot { int number;

当我编译并运行该程序时,它说:在函数“addToTheEnd”中:[警告]从不兼容的指针类型初始化//并且它指向这一行->knot\u 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;
}
#包括
#包括
类型定义结构结{
整数;
结构结*下一步;
}纽结;
int addToTheEnd(结**开始,int值){
纽结*新纽结=(纽结*)malloc(纽结大小);
如果(新的=空){
返回1;
}
新建->编号=数值;
新建->下一步=空;
if(start==NULL){
*开始=新建;
}否则{
结*电流=开始;
while(当前->下一步!=NULL){
当前=当前->下一步;
}
当前->下一步=新建;
}
返回0;
}
无效打印列表(结*开始){
结*电流=开始;
while(当前!=NULL){
printf(“%d”,当前->编号);
当前=当前->下一步;
}
}
无效清除列表(结*开始){
结*电流=开始;
while(当前!=NULL){
结t*trenutni=电流;
当前=当前->下一步;
免费(特鲁特尼);
}
}
int main(){
knot_t*start=NULL;
int i=0;
对于(i=0;i<10;i++){
if(添加结束(&start,i)){
printf(“失败\n”);
返回退出失败;
}
}
打印列表(开始);
清除列表(start);
返回退出成功;
}

该函数具有类型为
knot**
的参数
start

int addToTheEnd(knot_t **start, int value) {
knot_t *current = start;
而局部变量
current
的类型为
knot\u 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;
    }
这对我很有用:

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

start
具有类型
knot**
current
具有类型
knot*
。它们的指针类型不兼容,这意味着您做错了什么。
如果(start==NULL){
-->
如果(*start==NULL){
您想检查传递的指针是否为
NULL
,但是您正在检查本地指针(这将始终为您提供
true
),请注意
[警告]从不兼容的指针类型初始化实际上几乎总是一个错误而不是警告。它可能应该是
knot\t*current=*start;
,因为
start
是指向
knot\t
的指针,该指针来自
addToTheEnd(&start,i)
main()
,您需要取消对它的引用以获得
main
start
变量的值。您是否也解决了
if(start==NULL)
问题?是的,它有效,其他人也这么说。谢谢:)是的@Vlad来自莫斯科,就是这样。谢谢您的详细回答!СПаааааbаааааааааа