Arduino使用memcpy修改数组出现taking address of temporary array错误如何解决
错误原因
Arduino基于C标准开发,你所用的(bool []) {false, true, false, false, true}属于复合字面量,C标准规定这类临时数组的生命周期仅存在于当前表达式,不允许直接取其地址作为memcpy的数据源,因此会抛出"taking address of temporary array"编译错误。
解决方法
你可以选择以下任意一种方案修复问题:
- 方案1:直接逐个赋值(最稳妥,兼容性最强,适合短数组)
updateArea[0] = false; updateArea[1] = true; updateArea[2] = false; updateArea[3] = false; updateArea[4] = true;
- 方案2:先定义静态目标数组再调用memcpy(适合数组长度较长的场景,可维护性更高)
// 先定义存储目标值的常量数组 const bool targetVals[] = {false, true, false, false, true}; // 拷贝数据到updateArea,用sizeof自动计算数组长度,避免手动填写长度出错 memcpy(updateArea, targetVals, sizeof(updateArea));
内容的提问来源于stack exchange,提问作者noobie
相关产品推荐
相关产品推荐

