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.

图片描述

图片描述

哪位大佬可以回答一下,万分感谢

回答:

你的max和min是book类型,然而你的sort函数接受的参数类型是book的指针。更多的看不下去了

回答:

min 与max是变量,而 struct book p 要求传入指针, 传入 struct book p 尽量使用 '->' 传入变量使用' .'

以上是 C语言代码不知道哪错了 的全部内容, 来源链接: utcz.com/p/194072.html

回到顶部