C - 结构的新功能

时间:2013-11-06 19:39:33

标签: c

我试图弄清楚如何使用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;
}

2 个答案:

答案 0 :(得分: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};

答案 1 :(得分:-4)

您无法在函数中定义结构。

#include <stdio.h>

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

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

    printf("%d", adate.today);
}