从结构的C时的读数值 [英] Reading value from struct in C

查看:95
本文介绍了从结构的C时的读数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读一年从结构检查年份是否为闰年。

I'm trying to read a year from a struct to check whether that year is a leap year.

在此刻我的code是:

My code at the moment is:

typedef struct {
    int day;
    int month;
    int year;

} date;

和在读它的地方是:

  int is_date_valid(date *d) {

    printf("Year = %d\n", d.year);
    //Checking if year is a leap year
    if ( d->year%400 == 0)
        printf("%d is a leap year.\n", d.year);
    else if ( d->year%100 == 0)
        printf("%d is not a leap year.\n", d.year);
    else if ( d->year%4 == 0 )
        printf("%d is a leap year.\n", d.year);
    else
        printf("%d is not a leap year.\n", d.year); 



 return 0;
}

目前的时刻,年没有被读取的(年度正由用户输入的这个功能是由主叫)。我不知道如何调用今年从结构在这个函数中进行测试。我真的AP preciate任何帮助,任何人都可以给!谢谢

At the moment, the year is not being read in (the year is being entered by the user and this function is called from the main). I'm not sure how to call the year from the struct to be tested in this function. I'd really appreciate any help anyone can give! thanks

推荐答案

方法1

int is_date_valid(date *d)
{
    //access like d->year
}

main()
{
    date d;
    // get input here
    is_date_valid(&d)
}

方法2

int is_date_valid(int year)
{
    // use year here
}

main()
{
    date d;
    // get input here
    is_date_valid(d.year)
}

请注意:选择2只作品,如果在函数内部使用年

Note: Option 2 works only if use year inside the function

这篇关于从结构的C时的读数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆