C语言中的结构初始化问题
我似乎在设置结构内部数组的值时遇到了问题,编译器发出了一个无意义的错误: “{”标记之前应为主表达式 我理解一个结构必须“存在”才能接受值,它作为指向一个值的指针而存在。我希望你向我解释我做错了什么,以及如何实现我的目标C语言中的结构初始化问题,c,struct,C,Struct,我似乎在设置结构内部数组的值时遇到了问题,编译器发出了一个无意义的错误: “{”标记之前应为主表达式 我理解一个结构必须“存在”才能接受值,它作为指向一个值的指针而存在。我希望你向我解释我做错了什么,以及如何实现我的目标 struct EventCheckData { unsigned long refresh_time; unsigned long last_execution_ms; //Can also serve to delay at startup byte signal
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);
}
}
多谢各位
Paulo Neves您分配它的方式看起来像一个初始值设定项。您需要分配,请尝试复合文字:
clock_data->event_array[0] = (struct EventCheckData){ .refresh_time = 3000UL, ...};
(时钟数据->事件数组)[0]={.refresh\u time=3000UL、.last\u execution\u ms=0UL、.signal\u type=WATER\u PUMP\u ON};
不是初始化,而是赋值
并且不能在赋值中使用初始值设定项语法
使用C99,您应该能够使用复合文字,如
(clock_data->event_array)[0] = (struct EventCheckData){ .refresh_time = 3000UL, .last_execution_ms = 0UL, .signal_type = WATER_PUMP_ON};
没有任何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);
}
你在哪一行得到错误?你确定你能在C中做到吗?@Oli Charlesworth为什么不能?已经12年了,OP已经显示出c99的迹象:-)然后我从来没有意识到这是c99中的一个功能!哇,如此微妙。我理解,但我想我不会发现它