我有一个/两个我的方法时,我在main()
调用它的问题,我不确定为什么。不兼容类型的参数和未初始化在此函数
int main()
{
struct list * list;
list_init(list);
while(TRUE)
{
struct book book1;
printf("Enter title of book: ");
if(!getInput(book1.title, sizeof(book1.title)))
{
break;
}
printf("Enter author of book: ");
if(!getInput(book1.author, sizeof(book1.author)))
{
break;
}
printf("Enter dewey code of book: ");
if(!getInput(book1.deweycode, sizeof(book1.deweycode)))
{
break;
}
printf("Enter publisher code of book: ");
if(!getInput(book1.publisher, sizeof(book1.publisher)))
{
break;
}
printf("Enter ISBN code of book: ");
if(!getInput(book1.isbn, sizeof(book1.isbn)))
{
break;
}
list_add(list, &book1);
printf("\n");
}
printf("\n");
printf("Printing Linked List \n");
printList(list);
return EXIT_SUCCESS;
}
我收到与此代码的错误是:
“不兼容类型的‘list_add’参数2”
“预计‘结构的书*’,但参数的类型为“结构书'“。
我试图缓解这个问题,但改变book1
到&book1
作为建议的另一个问题,但是当我这样做,我得到一个警告:
“
list
在这个函数中未初始化”。
这里是我应该初始化的地方list
。
void list_init(struct list * list)
{
list = malloc(sizeof(*list));
if(list != NULL)
{
list->head = NULL;
list->num_books = 0;
}
}
这是这本书的结构:
struct book
{
char title[TITLELEN+1];
char author[AUTHORLEN+1];
char deweycode[DDCLEN+1];
char publisher[PUBLEN+1];
char isbn[ISBNLEN+1];
};
任何人都可以请帮助,并解释为什么列表不被初始化?我也试过list * list = malloc(sizeof(*list))
,但那给了我另一个错误。
如果你不打算从'list_init'返回一个节点,那么你需要将'list'的地址传递给'list_init'(例如'void list_init(struct list ** list)'。在我看来,提供灵活性来分配呼叫者的回报,或者简单地传递清单的地址,这两者都会更好。 (这是必需的,因为第一个节点的*地址*是列表*的地址*,您目前无法从'list_init'返回)您可以执行'list * list_init(struct list ** list)'while返回'list'包含两个基础。 –