C语言 错误:请求成员 ----- 在不是结构或联合的东西中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22351834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:52:26  来源:igfitidea点击:

error: request for member ----- in something not a structure or union

cstruct

提问by user3306583

I'm trying to access a member of a structure in a function. My struct looks like:

我正在尝试访问函数中结构的成员。我的结构看起来像:

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

} date;

The date is input by the user in the main. I am then trying to call the date in the following function:

日期由用户在 main.js 中输入。然后我尝试在以下函数中调用日期:

int is_date_valid(date *dob) {

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



 return 0;
}

This is giving the error in the title of this post. I understand that I am not accessing the year input into the struct but dont' know how to do this? Would appreciate any help! Thanks

这是这篇文章标题中的错误。我知道我没有访问结构中的年份输入,但不知道该怎么做?将不胜感激任何帮助!谢谢

回答by barak manos

Change dob.yearto dob->year.

更改dob.yeardob->year

Use .when you have a structure instance.

使用.时,你有一个结构实例。

Use ->when you have a pointer to a structure instance.

->当您有指向结构实例的指针时使用。