C中的结构体初始化问题

发布于 2024-12-06 00:53:14 字数 1200 浏览 1 评论 0原文

我似乎在设置结构内数组的值时遇到问题,编译器抛出一个无意义的错误:

“{”标记之前预期有主表达式

我知道结构必须“存在”才能接受值,并且它作为指向一的指针而存在。我希望你向我解释我做错了什么以及如何实现我的目标。

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

谢谢 保罗·内维斯

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:

expected primary-expression before '{' token

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-12-13 00:53:14

(clock_data->event_array)[0] = { .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON}; 不是初始化。是作业。

并且不能在赋值中使用初始化语法。

使用 C99,您应该能够使用复合文字,例如

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

(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

(clock_data->event_array)[0] = (struct EventCheckData){ .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON};
祁梦 2024-12-13 00:53:14

您分配它的方式看起来像一个初始值设定项。您需要分配,尝试复合文字:

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

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, ...};
避讳 2024-12-13 00:53:14

没有任何 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);
}

Without any C99 stuff you can simply use:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文