C - 新的结构

标签 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;
}

最佳答案

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};

关于C - 新的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821241/

相关文章:

c - 指向 char 的指针数组如何保存字符串而不是地址?

c - 为什么文件意外更改?

c - 循环中按值构造

c - 使用循环控制编写一个 C 程序以产生以下输出

c - 将 argv 传递到函数中不起作用

c - 如何传输以太网帧?

c - 如何在 C 中将 ASCII 值数组转换为整数

c - 指针并置使得 int_pointer 指向字符数据,反之亦然

C 读取文件到动态列表、动态字符串

c - malloced 指针中的内存泄漏?