1

我试图弄清楚如何使用 struct 但这段代码给了我很多错误。

#include <stdio.h>

int main(void)
{
    struct date
    {
        int today       =   6;
        int tomorrow    =   7;
        int threeDays   =   8;
    };

    struct date date;

    printf("%d", date.today);

    return 0;
}
4

2 回答 2

6
struct date
{
    int today       =   6;
    int tomorrow    =   7;
    int threeDays   =   8;
};

struct date date;

您不能为结构类型分配默认值。

您可以做的是使用正确的值初始化结构类型的对象:

struct date
{
    int today;
    int tomorrow;
    int threeDays;
};

struct date date = {6, 7, 8};
于 2013-11-06T19:40:57.420 回答
-4

您不能在函数中定义结构。

#include <stdio.h>

struct date { int today, tomorrow, threeDays; };

int main(void)
{
    struct date adate = { 6, 7, 8 };

    printf("%d", adate.today);
}
于 2013-11-06T19:42:14.853 回答