| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1468 人关注过本帖
标题:[已解决]结构数组在函数调用中指针类型的兼容转换问题
只看楼主 加入收藏
casio1374633
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-3-11
收藏
 问题点数:0 回复次数:6 
[已解决]结构数组在函数调用中指针类型的兼容转换问题
非常感谢各位一直的帮助,今天的程序稍微长点,有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 编辑 ]
搜索更多相关主题的帖子: 类型 结构 函数 指针 
2010-03-21 22:49
mikelr
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:161
注 册:2010-3-17
收藏
得分:0 
你把 ->  改成.


scanf("%s", res[i].name);
 
2010-03-21 23:10
casio1374633
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-3-11
收藏
得分:0 
回楼上,这个是肯定不对的既然使用了指针,就只能用->

静下心来,做点实事
2010-03-22 00:44
地狱无明火
Rank: 2
等 级:论坛游民
帖 子:62
专家分:71
注 册:2009-6-11
收藏
得分:0 
程序代码:
#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+i) -> price < (res+j-i) -> price)
#if  price_swap
                swap(&(res[j] -> price), &(res[j - 1] -> price));
#else
                swap((res+j), (res+j-i));
#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);
}

2010-03-22 06:55
casio1374633
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-3-11
收藏
得分:0 
回楼上,你是说把*res[] 改成*res吧, 因为我传递的是一个数组,所以在行参位置这么写.

而且你那么写是会报错的~



静下心来,做点实事
2010-03-22 08:38
地狱无明火
Rank: 2
等 级:论坛游民
帖 子:62
专家分:71
注 册:2009-6-11
收藏
得分:0 
上面的写法是传指针,还换了这些
printf("\n%s\n", res[i]->name);
printf("\n%s\n", (res+i) -> name);

一般选择传指针、数组其中一种
void input_info(restaurant res[]);
对应statement写法: scanf("%s", res[i].name);

void input_info(restaurant *res);
对应statement写法: scanf("%s", (res+i)->name);

void input_info(restaurant *res[]);//这样是三阶了

传数组:
程序代码:
#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[i].price < res[i].price)
#if  price_swap
                swap(&(res[j] -> price), &(res[j - 1] -> price));
#else
                swap(&res[j],&res[j-i]) ;/*小心形式*/
#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);
}




[ 本帖最后由 地狱无明火 于 2010-3-22 13:53 编辑 ]
2010-03-22 13:32
casio1374633
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2010-3-11
收藏
得分:0 
谢谢你,看来是我糊涂了.传指针我还是清楚的,不过*res[]传的是2维数组我忘记了.

res[][4] 等价 (*res)[4] 等价 res[5][4]

res[]等价*res.

静下心来,做点实事
2010-03-23 01:01
快速回复:[已解决]结构数组在函数调用中指针类型的兼容转换问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039697 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved