问题是将2维数组分配给1维数组
uint8_t Schedules[24][8]={
{0,1,1,1,1,1,1,0},
{0,2,2,2,2,2,2,0},
{0,3,3,3,3,3,3,0},
...
{0,24,24,24,24,24,24,0}
}
for (this_hour=0;this_hour<24;this_hour++){
for(this_section=1;this_section<7;this_section++)
{
buff[this_hour*9+this_section+2]=Schedules[this_hour][this_section];
}
}
结果必须为
buff[3..8]=1; /* Pseudocode; means buff[3]=1, buff[4]=1, ..., buff[8]=1 */
buff[9..11]=0;
buff[12..17]=2;
buff[18..20]=0,
...
但是,这是结果
我不知道我哪里错了。我十次检查循环,没有发现任何错误。我正在用keil 5中的freertos编写stm32 ARM。我确保buff仅在for循环中访问,而在代码中没有其他位置。它不是堆栈溢出,因为我给堆栈一个大数字
答案 0 :(得分:1)
像这样,数组从0开始,均值
时间表[x] [y] x,y是位置将出现在
下x * 8 + y
0 ...... 7从0,0〜0,7映射
8 ...... 15映射为1,0〜1,7
16 ...... 23映射为2,0〜2,7
........
@kevinmont ou是正确的问题,我编辑了问题并更正了,是的,您又是正确的,我有3个“ 0”的标头字节,然后是6个“ 1”,另外3个“ 0”,则6个“ 2”,然后是3“ 0”,依此类推。 –大卫6小时前
需要的数组大小为24 * 9 + 1 + 2
x * 9 + y + 2
2 ...... 9从0,0〜0,7映射
11 ...... 18映射为1,0〜1,7
20 ...... 27映射为2,0〜2,7
........
typedef int (*FPTR)();
int func1(){
cout<<"func1() being called\n";
}
int func2(){
cout<<"fun2() being called\n";
}
FPTR arr[2] = {&func1,&func2};
FPTR (*vptr)[2];
vptr=&arr;
cout<<"\n"<<vptr[0]<<endl;
cout<<"\n"<<vptr[0]()<<endl; // ERROR when trying to call the first function
答案 1 :(得分:0)
正如约书亚(Joshua)在评论中所说,内存存在问题,并且代码正确。我创建了一个局部变量,它正在工作。
cuprit是错误的声明
extern uint8_t Schedules[24][11];
此数组的定义为
extern uint8_t Schedules[24][8];
extern声明中数组的第二个参数错误。因此代码写入了调试中看不到的内存