如何修复:从不兼容的指针类型初始化
How to fix: initialization from incompatible pointer type
当我编译和 运行 程序时,它说:在函数 'addToTheEnd' 中:[警告] 从不兼容的指针类型初始化 // 它指向这一行 -> knot_t *当前=开始;
如何解决?我是 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;
}
该函数具有 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_;
另外,当函数在成功的情况下 return 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;
}
当我编译和 运行 程序时,它说:在函数 'addToTheEnd' 中:[警告] 从不兼容的指针类型初始化 // 它指向这一行 -> knot_t *当前=开始;
如何解决?我是 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;
}
该函数具有 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_;
另外,当函数在成功的情况下 return 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;
}