C中的结构体初始化问题
我似乎在设置结构内数组的值时遇到问题,编译器抛出一个无意义的错误:
“{”标记之前预期有主表达式
我知道结构必须“存在”才能接受值,并且它作为指向一的指针而存在。我希望你向我解释我做错了什么以及如何实现我的目标。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(clock_data->event_array)[0] = { .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON};
不是初始化。是作业。并且不能在赋值中使用初始化语法。
使用 C99,您应该能够使用复合文字,例如
(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
您分配它的方式看起来像一个初始值设定项。您需要分配,尝试复合文字:
The way you are assigning it looks like an initializer. You need assignment, try a compound literal:
没有任何 C99 的东西,你可以简单地使用:
Without any C99 stuff you can simply use: