| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1399 人关注过本帖
标题:函数返回结构体的出现诸多问题
只看楼主 加入收藏
Jaggle
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2013-9-15
结帖率:50%
收藏
已结贴  问题点数:18 回复次数:19 
函数返回结构体的出现诸多问题
开始是源于这样一个问题,从键盘输入十个同学的成绩,然后求出这是个成绩的平均值,最大值,最小值,要求使用调用函数的方式。

例题后面的答案是使用调用的函数return平均值,然后最大值和最小值使用全局变量,但是书上又说尽量不要使用全局变量,所以我想用别的办法让这个自己写的函数直接可以返回平均值、最大值、和最小值,于是想到了用这个函数返回数组或者定义一个结构体,结构体包含上面这个变量,然后返回到main函数。

 

可是关键字是,C语言可以返回结构体或者数组类型的变量吗?
首先我写的代码如下:
程序代码:
#include "stdio.h"
typedef struct
{
    float average;
    float max;
    float min;
}score;
score fixscore(float arr[10]){
    float max = arr[0];
    float min = arr[0];
    float sum = 0;
    float average = 0;
    int i = 0;
for(i=0;i<10;i++){
    if(arr[i]>=max) 
        max = arr[i];
    else if(arr[i]<=min)
        min=arr[i];
    sum = sum+arr[i];
}
    average = sum/10;
    score score1;
    score1.average    = average;
    score1.max        = max;
    score1.min        = min;

    return(score1);
}
    int main(){
        score score1;
        int i;
        float arr[10];
        for(i=0;i<10;i++)
        {
            scanf("%f",&arr[i]);
        }
        fixscore(arr[10]);
        printf("%f \n",score1.average);
        printf("%f \n",score1.max);
        printf("%f \n",score1.min);
    return 0;

    }


[ 本帖最后由 Jaggle 于 2013-9-20 18:10 编辑 ]
搜索更多相关主题的帖子: return 关键字 结构体 平均值 最大值 
2013-09-20 17:58
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:5 
通過指針在參數中返回所需要的結果

授人以渔,不授人以鱼。
2013-09-20 18:18
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:5 
可以返回一个指向结构体,或者数组的指针,对于数组,就是数组名

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-09-20 18:35
jg658237
Rank: 7Rank: 7Rank: 7
来 自:青藏高原
等 级:黑侠
帖 子:224
专家分:529
注 册:2013-8-8
收藏
得分:5 
scanf("%f",&arr[i]);    //scanf("%f",arr[i]);
你的这个程序有点看不懂  你注释一下吧

武功再高也怕菜刀.
2013-09-20 23:48
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct Statistics
{
    double average;
    double max;
    double min;
};

Statistics do_statistics(const double* score, int number);
void Pause(void);

int main(void)
{
    const double score[] = { 70.5, 82.0, 91.5, 63.0, 96.0, 53.5, 78.0, 88.0, 64.5, 90.5 };
    Statistics s = do_statistics(score, _countof(score));
    printf_s("Average = %.2f\n", s.average);
    printf_s("Max = %.2f\n", s.max);
    printf_s("Min = %.2f\n", s.min);
    Pause();
    return EXIT_SUCCESS;
}

Statistics do_statistics(const double* score, int number)
{
    Statistics s = { 0.0, score[0], score[0] };
    for (int index = 0; index < number; ++index)
    {
        s.average += score[index];
        if (score[index] > s.max)
        {
            s.max = score[index];
        }
        if (score[index] < s.min)
        {
            s.min = score[index];
        }
    }
    s.average = s.average / number;
    return s;
}

void Pause(void)
{
    fflush(stdin);
    getchar();
}

图片附件: 游客没有浏览图片的权限,请 登录注册



[ 本帖最后由 TonyDeng 于 2013-9-21 00:30 编辑 ]

授人以渔,不授人以鱼。
2013-09-20 23:53
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
換個版本看看:
程序代码:
#include <stdio.h>
#include <stdlib.h>

struct Statistics
{
    double average;
    double max;
    double min;
};

void do_statistics(const double* score, int number, Statistics* statistics);
void Pause(void);

int main(void)
{
    const double score[] = { 70.5, 82.0, 91.5, 63.0, 96.0, 53.5, 78.0, 88.0, 64.5, 90.5 };
    printf_s("struct Statistics size = 0x%04x\n", sizeof(Statistics));
    Statistics s = { 0.0, score[0], score[0] };
    do_statistics(score, _countof(score), &s);
    printf_s("s address = %p in main()\n", &s);
    printf_s("Average = %.2f\n", s.average);
    printf_s("Max = %.2f\n", s.max);
    printf_s("Min = %.2f\n", s.min);
    Pause();
    return EXIT_SUCCESS;
}

void do_statistics(const double* score, int number, Statistics* statistics)
{
    printf_s("s address = %p in do_statistics()\n", statistics);
    for (int index = 0; index < number; ++index)
    {
        statistics->average += score[index];
        if (score[index] > statistics->max)
        {
            statistics->max = score[index];
        }
        if (score[index] < statistics->min)
        {
            statistics->min = score[index];
        }
    }
    statistics->average = statistics->average / number;
}

void Pause(void)
{
    fflush(stdin);
    getchar();
}

图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 TonyDeng 于 2013-9-21 00:32 编辑 ]

授人以渔,不授人以鱼。
2013-09-21 00:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
再換一個版本:
程序代码:
#include <stdio.h>
#include <stdlib.h>

void do_statistics(const double* score, int number, double* average, double* max, double* min);
void Pause(void);

int main(void)
{
    const double score[] = { 70.5, 82.0, 91.5, 63.0, 96.0, 53.5, 78.0, 88.0, 64.5, 90.5 };
    double average = 0.0;
    double max = score[0];
    double min = score[0];
    do_statistics(score, _countof(score), &average, &max, &min);
    printf_s("Average = %.2f\n", average);
    printf_s("Max = %.2f\n", max);
    printf_s("Min = %.2f\n", min);
    Pause();
    return EXIT_SUCCESS;
}

void do_statistics(const double* score, int number, double* average, double* max, double* min)
{
    for (int index = 0; index < number; ++index)
    {
        *average += score[index];
        if (score[index] > *max)
        {
            *max = score[index];
        }
        if (score[index] < *min)
        {
            *min = score[index];
        }
    }
    *average = *average / number;
}

void Pause(void)
{
    fflush(stdin);
    getchar();
}


[ 本帖最后由 TonyDeng 于 2013-9-21 00:29 编辑 ]

授人以渔,不授人以鱼。
2013-09-21 00:24
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
你比較一下三個版本各有什麽特點吧。比較的角度:一是代碼的書寫,哪個函數簡了,哪個函數書寫量大了,那些奇怪的符號哪種看上去更順眼;二是留意我為什麽要貼那兩幅圖,追蹤那兩個變量的地址是什麽意思。

授人以渔,不授人以鱼。
2013-09-21 00:40
我叫沃恩
Rank: 12Rank: 12Rank: 12
来 自:Asia
等 级:贵宾
威 望:10
帖 子:1234
专家分:3865
注 册:2013-3-29
收藏
得分:5 
T版用的是Visual Studio2010或2012,支持C99特性,所以需要用Visual studio2010看这个程序的执行效果!!

T版,第一个版本的代码,好像少了前三个输出结果的代码!

两幅图中最大的区别是s的地址不一样,第二个是把通过调用指针,使得函数可以改变s的内容!第一个则是在调用函数时重新开辟了一个空间!

我觉得第三个比较容易理解!虽然定义的指针变量有点多!

T版,是不是这样?指教,,,,

因为我是菜鸟,所以应该被骂! 细节+坚持=成功!
2013-09-21 09:34
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
是的,第一幅圖是我修改代碼後加上去的,沒貼出修改代碼,那幾個輸出其實跟第二個差不多。

函數的return,其實是在棧上把局部變量复制(用類似memcpy()那樣的方式)給調用處的接收變量,所以兩處的地址是不一樣的,亦即在棧上佔用兩份空間,當結構體的尺寸不大時,這種消耗不明顯,但實際上复制內存,既要消耗內存,也要消耗時間,如何選擇,要看具體情況了。

本來,要返回例题中這樣幾個數據,在邏輯上是不需要特意構造一個結構體類型的,因爲類型意味著某種現實模擬,在這裡三個返回値其實是獨立的,被强制捆綁在一個結構體中邏輯上顯得比較突兀,所以整體看起來第三個版本反而更清晰,也是傳統的做法。

授人以渔,不授人以鱼。
2013-09-21 09:50
快速回复:函数返回结构体的出现诸多问题
数据加载中...
 
   



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

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