初始化C中的特定成员

时间:2018-10-05 00:21:15

标签: pointers struct

我想定义结构

    #include <stdio.h>
#include <stdint.h>
#include <time.h>
//#include "templog.h"
#define DATE_SIZE      5
 typedef struct {
    int id;
    int year;
    int month;
    int mday;
    float min_temp, max_temp;
    } tempValue_t

数据是

tempValue_t temps[DATE_SIZE] =
    { 0, { .tm_year = 2015, .tm_mon = 0, .tm_mday = 1 }, 19.26, 20.76 },

我收到消息

error field name not in record or union initializer

2 个答案:

答案 0 :(得分:0)

您正在尝试使用错误的成员名称初始化结构。要初始化数组中的第一个结构,您可以执行以下操作:

tempValue_t temps[DATE_SIZE] = {
    {0, .year = 2015, .month = 0, .mday = 1, 19.26, 20.76}
};

答案 1 :(得分:0)

最后发现,即使可以使用(。)初始化嵌套结构,也可以初始化结构成员。