嵌套结构体按引用传参至函数后值未更新的问题排查
嵌套结构体指针传递后值无法更新的问题
核心问题
将嵌套结构体按引用传递给函数后,嵌套结构体的成员值无法更新,确认问题出在指针参数定义与成员访问的语法错误上(已有主函数内逻辑正常的版本,排除业务逻辑问题)。
结构体定义
struct alarm_event { int1 alarm_state; int8 Hours; int8 Minutes; int8 Seconds; }; struct stamp { int1 record; int8 Hours; int8 Minutes; int8 Seconds; }; struct sensor { char descriptor[2]; //output description int16 raw_value; //raw adc value 10bit ADC 0-1023 int8 channel; //adc channel sensor wired into int16 alarm_limit; struct alarm_event alarm; struct stamp alarm_record[2]; int1 previous; };
主函数中的结构体数组初始化
void main() { struct sensor weather_station[4] = {{"L",0,0,200,{0,0,0,0},{{0,0,0,0},{0,0,0,0}},0}, {"T",0,0,300,{0,0,0,0},{{0,0,0,0},{0,0,0,0}},0}, {"P",0,0,400,{0,0,0,0},{{0,0,0,0},{0,0,0,0}},0}, {"H",0,0,500,{0,0,0,0},{{0,0,0,0},{0,0,0,0}},0} }; }
触发逻辑与函数调用
当ADC值超过告警阈值时,更新告警时间并调用Sample_2函数:
if(weather_station[i].raw_value >= weather_station[i].alarm_limit) { if(weather_station[i].alarm.alarm_state != 1) { weather_station[i].alarm.alarm_state = 1; weather_station[i].alarm.Hours = Hours; weather_station[i].alarm.Minutes = Minutes; weather_station[i].alarm.Seconds = Seconds; // 调用Sample_2,传入当前sensor元素的地址 Sample_2(&weather_station[i]); } }
原Sample_2函数的错误点
原函数的参数定义与成员访问语法均存在错误:
// 参数定义错误:这里声明的是指向sensor的指针数组,但实际传入的是单个sensor指针 void Sample_2(struct sensor *weather_station[], int i) { int1 updated = 0, allones = 0; int j = 0, k = 0; // 原代码未声明j、k,需补充变量声明 while(!updated) { // 解引用语法错误:.优先级高于*,且weather_station[i]是指针,需用->访问成员 if( *weather_station[i].alarm_record[j].record == 0 && allones == 0 ) { *weather_station[i].alarm_record[j].record = 1; *weather_station[i].alarm_record[j].Hours = *weather_station[i].alarm.Hours; *weather_station[i].alarm_record[j].Minutes = *weather_station[i].alarm.Minutes; *weather_station[i].alarm_record[j].Seconds = *weather_station[i].alarm.Seconds; updated = 1; } if(k == 1) *weather_station[i].previous = 0; if ( *weather_station[i].alarm_record[k].record == 1 && allones == 1 && *weather_station[i].previous == 0 ) { *weather_station[i].alarm_record[k].record = 1; *weather_station[i].alarm_record[k].Hours = *weather_station[i].alarm.Hours; *weather_station[i].alarm_record[k].Minutes =*weather_station[i].alarm.Minutes; *weather_station[i].alarm_record[k].Seconds = *weather_station[i].alarm.Seconds; updated = 1; *weather_station[i].previous = 1; if(k==1) *weather_station[i].previous = 0; } j++; if (j > 1) { allones = 1; } if(j > 2) { k++; } } }
错误分析
- 参数类型不匹配:调用时传入的是
&weather_station[i](单个struct sensor*),但函数参数声明为struct sensor *weather_station[](指针数组),导致内部访问逻辑完全错误。 - 成员访问语法错误:
.的优先级高于*,*weather_station[i].alarm_record[j].record会被解析为*(weather_station[i].alarm_record[j].record),但weather_station[i]是指针类型,必须用->访问成员,而非.。 - 冗余参数:传入的
i完全多余,因为指针已经直接指向目标sensor元素。
修正后的Sample_2函数
// 参数改为单个struct sensor指针,无需传入i void Sample_2(struct sensor *sensor_ptr) { int1 updated = 0, allones = 0; int j = 0, k = 0; while(!updated) { // 使用->访问指针指向的结构体成员 if( sensor_ptr->alarm_record[j].record == 0 && allones == 0 ) { sensor_ptr->alarm_record[j].record = 1; sensor_ptr->alarm_record[j].Hours = sensor_ptr->alarm.Hours; sensor_ptr->alarm_record[j].Minutes = sensor_ptr->alarm.Minutes; sensor_ptr->alarm_record[j].Seconds = sensor_ptr->alarm.Seconds; updated = 1; } if(k == 1) sensor_ptr->previous = 0; if ( sensor_ptr->alarm_record[k].record == 1 && allones == 1 && sensor_ptr->previous == 0 ) { sensor_ptr->alarm_record[k].record = 1; sensor_ptr->alarm_record[k].Hours = sensor_ptr->alarm.Hours; sensor_ptr->alarm_record[k].Minutes = sensor_ptr->alarm.Minutes; sensor_ptr->alarm_record[k].Seconds = sensor_ptr->alarm.Seconds; updated = 1; sensor_ptr->previous = 1; if(k==1) sensor_ptr->previous = 0; } j++; if (j > 1) { allones = 1; } if(j > 2) { k++; } } }
修正说明
- 参数改为
struct sensor *sensor_ptr,直接接收目标结构体的指针,类型匹配。 - 使用
->访问指针指向的结构体成员,语法正确,且能直接修改原结构体的成员值。 - 移除冗余的
i参数,代码更简洁。
内容的提问来源于stack exchange,提问作者Vitaliy
相关产品推荐
相关产品推荐

