C语言代码不知道哪错了
1.想用结构数组执行查找书籍的操作,查找定价最高和最低的书名称并输出,但不知道错误在哪里
2.代码
#include <stdio.h>
#include <stdlib.h>
void sort(struct book *p,struct book *pmax,struct book *pmin,int n);
struct book
{
char name[20];
float price;
}books[20],max,min;
int main()
{
int n,i;
printf("Input n:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input name of the book\n");
scanf("%s",books[i].name);
printf("please input the price\n");
scanf("%f",&books[i].price);
}
sort(books,max,min,n);
printf("max\tname:%s\nprice:%.2f\n",max.name,max.price);
printf("min\tname:%s\nprice:%.2f\n",min.name,min.price);
system("pause");
return 0;
}
void sort(struct book *p,struct book *pmax,struct book *pmin,int n)
{
int i=0;
*pmax=*p[0];
*pmin=*p[0];
for(;i<n;i++)
{
if(*(p+i).price>*pmax.price)
*pmax=*(p+i);
if(*(p+i).price<*pmin.price)
*pmin=*(p+i);
}
}
3.错误
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:22:5: error: incompatible type for argument 2 of 'sort'
sort(books,max,min,n);
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:3:6: note: expected 'struct book *' but argument is of type 'struct book'
void sort(struct book *p,struct book *pmax,struct book *pmin,int n);
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:22:5: error: incompatible type for argument 3 of 'sort'
sort(books,max,min,n);
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:3:6: note: expected 'struct book *' but argument is of type 'struct book'
void sort(struct book *p,struct book *pmax,struct book *pmin,int n);
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c: At top level:
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:28:6: error: conflicting types for 'sort'
void sort(struct book *p,struct book *pmax,struct book *pmin,int n)
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:3:6: note: previous declaration of 'sort' was here
void sort(struct book *p,struct book *pmax,struct book *pmin,int n);
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c: In function 'sort':
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:31:11: error: invalid type argument of unary '*' (have 'struct book')
*pmax=*p[0];
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:32:11: error: invalid type argument of unary '*' (have 'struct book')
*pmin=*p[0];
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:36:18: error: request for member 'price' in something not a structure or union
if(*(p+i).price>*pmax.price)
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:36:30: error: request for member 'price' in something not a structure or union
if(*(p+i).price>*pmax.price)
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:38:18: error: request for member 'price' in something not a structure or union
if(*(p+i).price<*pmin.price)
^
E:\CodeBlocks\新建文件夹\查找书籍a\main.c:38:30: error: request for member 'price' in something not a structure or union
if(*(p+i).price<*pmin.price)
^
Process terminated with status 1 (0 minute(s), 0 second(s))
9 error(s), 3 warning(s) (0 minute(s), 0 second(s))
4.
哪位大佬可以回答一下,万分感谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的max和min是book类型,然而你的sort函数接受的参数类型是book的指针。更多的看不下去了
min 与max是变量,而 struct book p 要求传入指针, 传入 struct book p 尽量使用 '->' 传入变量使用' .'