当我将一个结构体传递给一个函数时,出现了错误:预期为“struct book”,但参数类型为“struct book”。为什么会这样?
错误信息如下:
#include <stdio.h>
#include <string.h>
struct book
{
int id;
char title[50];
};
int showBooks(struct book x);
int main()
{
struct book
{
int id;
char title[50];
};
struct book book1,book2;
book1.id = 2;
book2.id = 3;
strcpy(book1.title, "c programming");
strcpy(book2.title, "libc refrence");
printf("Book\t\tID\n");
showBooks(book1);
showBooks(book2);
}
int showBooks(struct book x)
{
printf("%s\t%d\n", x.title, x.id);
}
错误信息如下:
错误:
30:12:错误:‘showBooks’的参数1类型不兼容
showBooks(book1);10:5:注意:期望的是‘struct book’类型,但实参是‘struct book’类型 int showBooks(struct book x);
31:12:错误:‘showBooks’的参数1类型不兼容
showBooks(book2);10:5:注意:期望的是‘struct book’类型,但实参是‘struct book’类型 int showBooks(struct book x);
这里出了什么问题?