[已解决]结构数组在函数调用中指针类型的兼容转换问题
非常感谢各位一直的帮助,今天的程序稍微长点,有100行,麻烦各位了.题目要求,输入5家餐厅的信息情况,并且根据餐厅的价格排序输出.
餐厅信息:1:餐厅名称,2:餐厅地址,3:餐厅平均价格,4:餐厅等级
下面是程序
========================================
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define price_swap 0
typedef struct restaurant {
char name[30];
char address[30];
float price;
char get_choice;
} restaurant;
void prt_info(void);
void input_info(restaurant *res[]);
void sort_price(restaurant *res[]);
void swap(restaurant *p, restaurant *q);
void wrt(restaurant *res[]);
void error_input_choice(void);
int main(void)
{
restaurant res[5];
prt_info();
input_info(res);
sort_price(res);
wrt(res);
return 0;
}
void prt_info(void)
{
printf("\n%s\n",
"This is a demo for bubble the restaurant\n"
"need you type five restaurant info.\n"
"example info is :\n"
"name:foo\naddress:foo address\nprice:100\nchoice:A\n"
"choice A for good, B for normal, C for enough, D for bad, E for very_bad\n"
"Thank you!\n\n");
}
void input_info(restaurant *res[]) /* <-----报错位置....*/
{
int i;
for (i = 0; i < 5; i++) {
printf("\nrestaurant number %d\nname:", i + 1);
scanf("%s", res[i] -> name);
printf("\naddress:");
scanf("%s", res[i] -> address);
printf("\nprice:");
scanf("%f", &res[i] -> price);
getchar();
printf("\nchoice:");
while (isspace(res[i] -> get_choice = getchar()))
;
if ( res[i] -> get_choice < 'A' || res[i] -> get_choice > 'D')
error_input_choice();
}
}
void sort_price(restaurant *res[]) /* <-----报错位置....*/
{
int i, j;
for (i = 0; i < 4; i++)
for (j = 4; j > i; j--) {
if (res[j] -> price < res[j - 1] -> price)
#if price_swap
swap(&(res[j] -> price), &(res[j - 1] -> price));
#else
swap(res[j], res[j - 1]);
#endif
}
}
void swap(restaurant *p, restaurant *q)
{
restaurant *tmp;
tmp = p;
p = q;
q = tmp;
}
void wrt(restaurant *res[]) /* <-----报错位置....*/
{
int i;
printf("\n%s\n",
"here is the sort\n"
"-----------------\n");
for (i = 0; i < 5; i++) {
printf("\n%s\n", res[i] -> name);
printf("\n%s\n", res[i] -> address);
printf("\n%f\n", res[i] -> price);
printf("\n%c\n", res[i] -> get_choice);
printf("\n-----------------------------\n");
}
}
void error_input_choice(void)
{
printf("\n%s\n",
"This is a error for choice input\n"
"You should read the instruction again.\n");
exit(1);
}gcc没报错,只是警告,说:在给input_info , wrt sort三个函数传地址时候,在不兼容的指针类型间转换.我想请问一下,我简历结构数组的话,能否这样使用传递?因为我之前成功的时候,使用的是传值调用.书上也说过传值调用在结构很大的时候会很占内存.另外如果是在定义一个指针传地址.那么就只能使用res[i] -> *** . 但是我的name和address本身也是指针.请问在输入和输出时候,单纯这么写res[i] -> name 可以吗?如果不正确,应该怎么写?
谢谢各位
===================谢谢各位的帮助============
完成后代码如下:
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct restaurant {
char name[30];
char address[30];
float price;
char get_choice;
} restaurant;
void prt_info(void);
void input_info(restaurant res[]);
void sort_price(restaurant res[]);
void swap(restaurant *p, restaurant *q);
void wrt(restaurant res[]);
void error_input_choice(void);
int main(void)
{
restaurant res[5];
prt_info();
input_info(res);
sort_price(res);
wrt(res);
return 0;
}
void prt_info(void)
{
printf("\n%s\n",
"This is a demo for bubble the restaurant\n"
"need you type five restaurant info.\n"
"example info is :\n"
"name:foo\naddress:foo address\nprice:100\nchoice:A\n"
"choice A for good, B for normal, C for enough, D for bad, E for very_bad\n"
"Thank you!\n\n");
}
void input_info(restaurant res[]) /* <-----报错位置....*/
{
int i;
for (i = 0; i < 5; i++) {
printf("\nrestaurant number %d\nname:", i + 1);
scanf("%s", res[i].name);
printf("\naddress:");
scanf("%s", res[i].address);
printf("\nprice:");
scanf("%f", &res[i].price);
getchar();
printf("\nchoice:");
while (isspace(res[i].get_choice = getchar()))
;
if ( res[i].get_choice < 'A' || res[i].get_choice > 'D')
error_input_choice();
}
}
void sort_price(restaurant res[])
{
int i, j;
for (i = 0; i < 4; ++i)
for (j = 4; j > i; --j)
if (res[j].price < res[j - 1].price)
swap(&res[j - 1],&res[j]) ;/*小心形式*/
}
void swap(restaurant *p, restaurant *q)
{
restaurant tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
void wrt(restaurant res[]) /* <-----报错位置....*/
{
int i;
printf("\n%s\n",
"here is the sort\n"
"-----------------\n");
for (i = 0; i < 5; i++) {
printf("\n%s\n", res[i].name);
printf("\n%s\n", res[i].address);
printf("\n%f\n", res[i].price);
printf("\n%c\n", res[i].get_choice);
printf("\n-----------------------------\n");
}
}
void error_input_choice(void)
{
printf("\n%s\n",
"This is a error for choice input\n"
"You should read the instruction again.\n");
exit(1);
}[ 本帖最后由 casio1374633 于 2010-3-23 01:43 编辑 ]








