简体   繁体   English

C中的结构初始化问题

[英]Struct initialization problem in C

I seem to be having a problem setting the values of an array inside a structure with a meaningless error spat out of the compiler: 我似乎在设置结构中的数组值时出现问题,并且编译器中出现无意义的错误spat:

expected primary-expression before '{' token 在'{'标记之前预期的primary-expression

I understand that a structure must "exist" to accept values, and it exists as a pointer to one. 我理解结构必须“存在”才能接受值,并且它作为指向一个的指针存在。 I would like you to explain to me what i am doing wrong and how to achieve my objective. 我希望你向我解释我做错了什么以及如何实现我的目标。

struct EventCheckData {
  unsigned long refresh_time;
  unsigned long last_execution_ms; //Can also serve to delay at startup
  byte signal_type;
};

struct ClockData {
    struct EventCheckData event_array[4];
    byte event_count;
    unsigned long last_absolute_time;
    UISignal *warning_signals;
}; 



 void ResetClock(UISignal *warning_signal, struct ClockData *clock_data, unsigned long absolute_time) { 
    if(SignalCheckValue(warning_signal, RESET_CLOCK, 1)) {
        extern volatile unsigned long timer0_overflow_count;
        timer0_overflow_count = 0;
        clock_data->last_absolute_time = absolute_time;
        clock_data->event_count = 3;
        (clock_data->event_array)[0] = { .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON};
//      clock_data->event_array[1] = {10000UL, 0UL, EXPORT_LOG};
//      clock_data->event_array[2] = {100000UL, 0UL, EXTERNAL_CONNECTION};
        SignalSet(warning_signal, RESET_CLOCK, 0);
    }
}

Thank you Paulo Neves 谢谢Paulo Neves

The way you are assigning it looks like an initializer. 您分配它的方式看起来像初始化程序。 You need assignment, try a compound literal: 您需要分配,尝试复合文字:

clock_data->event_array[0] = (struct EventCheckData){ .refresh_time = 3000UL, ...};

(clock_data->event_array)[0] = { .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON}; is not initialization. 不是初始化。 It is assignment. 这是任务。

And you cannot use initializer syntax in assignment. 并且您无法在赋值中使用初始化程序语法。

With C99, you should be able to use a compound literal, like 使用C99,您应该能够使用复合文字,例如

(clock_data->event_array)[0] = (struct EventCheckData){ .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON};

Without any C99 stuff you can simply use: 没有任何C99的东西你可以简单地使用:

void ResetClock(UISignal *warning_signal, struct ClockData *clock_data, unsigned long absolute_time) { 
if(SignalCheckValue(warning_signal, RESET_CLOCK, 1)) {
  extern volatile unsigned long timer0_overflow_count;
  timer0_overflow_count = 0;
  clock_data->last_absolute_time = absolute_time;
  clock_data->event_count = 3;
  {
    struct EventCheckData a[]={ {3000UL, 0UL, WATER_PUMP_ON},
                                {10000UL, 0UL, EXPORT_LOG},
                                {100000UL, 0UL, EXTERNAL_CONNECTION}};
    memcpy(clock_data->event_array,a,sizeof a);
  }
  SignalSet(warning_signal, RESET_CLOCK, 0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM