error: incompatible type for argument 1 of 'push'
error: incompatible type for argument 1 of 'push'
我正在尝试建立一个链表,但我无法通过错误。有人可以帮我理解发生了什么事吗?我已经更改了结构的类型,但没有任何变化。
我正在尝试建立一个链表,但我无法通过错误。有人可以帮我理解发生了什么事吗?我已经更改了结构的类型,但没有任何变化。
/*gcc -o lista.exe lista.c -Wall -pedantic -Wextra*/
#include <stdio.h>
#include <stdlib.h>
struct list
{
int info;
struct list *next;
};
void push(struct list, int);
int main(int argc, char *argv[])
{
struct list *list;
argc = argc;
argv = argv;
/*Start list*/
list = (struct list *)malloc(sizeof(struct list));
list->info = 5;
list->next = NULL;
push(&list, 70);
return 0;
}
void push(struct list **list, int info)
{
struct list *new;
new = (struct list *)malloc(sizeof(struct list));
new->info = info;
new->next = NULL;
*list->next = new;
}
lista.c: In function 'main':
lista.c:25:3: error: incompatible type for argument 1 of 'push'
lista.c:11:6: note: expected 'struct list' but argument is of type 'struct list **'
lista.c: At top level:
lista.c:30:6: error: conflicting types for 'push'
lista.c:11:6: note: previous declaration of 'push' was here
lista.c: In function 'push':
lista.c:37:8: error: request for member 'next' in something not a structure or union
push
的函数 prototype/declaration 与 push
的函数定义不匹配。在顶部,更改
void push(struct list, int);
到
void push(struct list**, int);
或者,您可以删除 push
的函数原型并将其定义移至 main
上方。编译器从上到下读取文件,因此在 main
中,当您调用 push(&list, 70);
时,如果编译器不知道与该签名匹配的函数,则会报错。由于您的原型是错误的,它没有看到匹配 return 类型的 void
和参数 struct list**, int
的函数,因此它会生成您看到的错误。
顺便说一句,我假设你有
argc = argc;
argv = argv;
抑制来自未使用变量的警告。如果您不打算使用 argc
或 argv
,您可以将 main
签名更改为 int main(void){ ... }
并完全处理它们。
编译器看到声明
void push(struct list, int);
之后遇到这个调用
push(&list, 70);
其中第一个参数的类型为 struct list **
。并且它发出一个错误,函数参数的类型 struct list
和参数表达式的类型 struct list **
不兼容。
没有定义的函数声明与main函数之后有定义的函数声明不一致。
但无论如何函数定义都是无效的。
函数内的这个语句
*list->next = new;
不正确。相当于
*( list->next ) = new;
其中类型为 struct list **
的指针 list
指向的指针不是结构对象。即指向的指针没有数据成员next
。编译器对此进行了报告
lista.c:37:8: error: request for member 'next' in something not a
structure or union
该函数应按以下方式声明
int push( struct list **, int );
并定义为
int push( struct list **list, int info )
{
struct list *new_node = malloc( sizeof( struct list ) );
int success = new_node != NULL;
if ( success )
{
new_node->info = info;
new_node->next = *list;
*list = new_node;
}
return success;
}
并且在main中不需要在函数外分配头节点push
。
你应该写
int main( void )
{
struct list *list = NULL;
push( &list, 5 );
push( &list, 70 );
return 0;
}
我正在尝试建立一个链表,但我无法通过错误。有人可以帮我理解发生了什么事吗?我已经更改了结构的类型,但没有任何变化。
我正在尝试建立一个链表,但我无法通过错误。有人可以帮我理解发生了什么事吗?我已经更改了结构的类型,但没有任何变化。
/*gcc -o lista.exe lista.c -Wall -pedantic -Wextra*/
#include <stdio.h>
#include <stdlib.h>
struct list
{
int info;
struct list *next;
};
void push(struct list, int);
int main(int argc, char *argv[])
{
struct list *list;
argc = argc;
argv = argv;
/*Start list*/
list = (struct list *)malloc(sizeof(struct list));
list->info = 5;
list->next = NULL;
push(&list, 70);
return 0;
}
void push(struct list **list, int info)
{
struct list *new;
new = (struct list *)malloc(sizeof(struct list));
new->info = info;
new->next = NULL;
*list->next = new;
}
lista.c: In function 'main':
lista.c:25:3: error: incompatible type for argument 1 of 'push'
lista.c:11:6: note: expected 'struct list' but argument is of type 'struct list **'
lista.c: At top level:
lista.c:30:6: error: conflicting types for 'push'
lista.c:11:6: note: previous declaration of 'push' was here
lista.c: In function 'push':
lista.c:37:8: error: request for member 'next' in something not a structure or union
push
的函数 prototype/declaration 与 push
的函数定义不匹配。在顶部,更改
void push(struct list, int);
到
void push(struct list**, int);
或者,您可以删除 push
的函数原型并将其定义移至 main
上方。编译器从上到下读取文件,因此在 main
中,当您调用 push(&list, 70);
时,如果编译器不知道与该签名匹配的函数,则会报错。由于您的原型是错误的,它没有看到匹配 return 类型的 void
和参数 struct list**, int
的函数,因此它会生成您看到的错误。
顺便说一句,我假设你有
argc = argc;
argv = argv;
抑制来自未使用变量的警告。如果您不打算使用 argc
或 argv
,您可以将 main
签名更改为 int main(void){ ... }
并完全处理它们。
编译器看到声明
void push(struct list, int);
之后遇到这个调用
push(&list, 70);
其中第一个参数的类型为 struct list **
。并且它发出一个错误,函数参数的类型 struct list
和参数表达式的类型 struct list **
不兼容。
没有定义的函数声明与main函数之后有定义的函数声明不一致。
但无论如何函数定义都是无效的。
函数内的这个语句
*list->next = new;
不正确。相当于
*( list->next ) = new;
其中类型为 struct list **
的指针 list
指向的指针不是结构对象。即指向的指针没有数据成员next
。编译器对此进行了报告
lista.c:37:8: error: request for member 'next' in something not a structure or union
该函数应按以下方式声明
int push( struct list **, int );
并定义为
int push( struct list **list, int info )
{
struct list *new_node = malloc( sizeof( struct list ) );
int success = new_node != NULL;
if ( success )
{
new_node->info = info;
new_node->next = *list;
*list = new_node;
}
return success;
}
并且在main中不需要在函数外分配头节点push
。
你应该写
int main( void )
{
struct list *list = NULL;
push( &list, 5 );
push( &list, 70 );
return 0;
}