| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1324 人关注过本帖
标题:请教一道题,关于结构体作为函数参数传递的
只看楼主 加入收藏
cwde
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-3-31
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:4 
请教一道题,关于结构体作为函数参数传递的
题目是这样的,计算日期(含年,月,日)是该年的第几天,大家帮忙帮我看看程序
#include<stdio.h>
int str(struct boy a);
int main(void)
{
    struct boy{
        int year;
        int month;
        int day;}a={2004,11,30};
    int w;
    w=str(a);
    printf("%d",w);}
int str(struct boy a)
{
        int x[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int count=0,i;
        count=a.day;
       printf("%d\n",count);
        for(i=0;i<a.month-1;i++)
            count+=x[i];
        printf("%d\n",count);
        if(a.month>2&&((a.year%4==0)&&(a.year%100!=0)||(a.year%400==0)))
        count=count+1;   
        printf("%d\n",count);
        return (count);
}

把结构作为参数传进去,就报错了,晕,大家帮忙看看哪错了呀?
搜索更多相关主题的帖子: 结构体 
2011-04-30 22:26
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:5 
结构体类型定义不能放在主函数中,否则他是局部的,只能在主函数有效。
2011-04-30 22:29
为我留住记忆
Rank: 4
来 自:北京
等 级:业余侠客
帖 子:130
专家分:226
注 册:2011-4-30
收藏
得分:5 
好像定义的struct(){};
一定加分号

学习c是为了自己更强大。。。
2011-04-30 22:33
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:5 
程序代码:

#include<stdio.h>

struct boy {
        int year;
        int month;
        int day;
}a={2004,11,30};

int main (void) {
        int str(struct boy a);
        printf("%d\n",str(a));
        return 0;
}
int str(struct boy a) {
        int x[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int count=0,i;
        for(i=0;i<a.month;i++)
                count+=x[i];
        if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0))
                count=count+1;
        return count;
}


[ 本帖最后由 ansic 于 2011-4-30 23:43 编辑 ]

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-04-30 23:42
hjywyj
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:1114
专家分:2611
注 册:2010-4-14
收藏
得分:5 
你的结构体不是全局的,只是main函数中有效。
程序代码:
struct boy{
        int year;
        int month;
        int day;}a;
int str(struct boy a)
{       int x[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int count=0,i;
        count=a.day;
        for(i=0;i<a.month-1;i++)
        count+=x[i];
        if(a.month>2&&((a.year%4==0)&&(a.year%100!=0)||(a.year%400==0)))
        count=count+1;   
        return (count);
}
main()
{
        scanf("%d%d%d",&a.year,&a.month,&a.day);
    int w;
    w=str(a);
    printf("%d",w);
getch();
}

2011-05-01 08:10
快速回复:请教一道题,关于结构体作为函数参数传递的
数据加载中...
 
   



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

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