gpt4 book ai didi

c - 数组赋值有奇怪的结果

转载 作者:行者123 更新时间:2023-11-30 19:01:25 25 4
gpt4 key购买 nike

问题是将二维数组分配给一维数组

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,
...

然而,这就是结果

debugger result Image

我不知道我错在哪里。我检查了十次循环,没有发现任何错误。我正在 keil 5 中的 freertos 中为 stm32 ARM 编写。我确保仅在 for 循环中访问 buff,而不是代码中的其他位置。这不是堆栈溢出,因为我给了堆栈一个很大的数字

最佳答案

像这样,数组从0开始,均值

Schedules[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”的 header 字节,然后是 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映射

......

#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
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,4,4,4,4,4,4,0},
{0,5,5,5,5,5,5,0},
{0,6,6,6,6,6,6,0},
{0,7,7,7,7,7,7,0},
{0,8,8,8,8,8,8,0},
{0,9,9,9,9,9,9,0},
{0,10,10,10,10,10,10,0},
{0,11,11,11,11,11,11,0},
{0,12,12,12,12,12,12,0},
{0,13,13,13,13,13,13,0},
{0,14,14,14,14,14,14,0},
{0,15,15,15,15,15,15,0},
{0,16,16,16,16,16,16,0},
{0,17,17,17,17,17,17,0},
{0,18,18,18,18,18,18,0},
{0,19,19,19,19,19,19,0},
{0,20,20,20,20,20,20,0},
{0,21,21,21,21,21,21,0},
{0,22,22,22,22,22,22,0},
{0,23,23,23,23,23,23,0},
{0,24,24,24,24,24,24,0}
};

int main(){
int this_hour,this_section,i;
uint8_t buff[192]={0};//24*8
for (this_hour=0;this_hour<24;this_hour++){
for(this_section=0;this_section<8;this_section++)
{
buff[this_hour*8+this_section]=Schedules[this_hour][this_section];
}
}
for(i=0;i<192;i++){

if(i!=0 && i%8==0){
printf("\n");
}
printf("%d",buff[i]);
}

printf("\n------------------------------------\n");


uint8_t buff2[219]={0};//24*9+1+2
for (this_hour=0;this_hour<24;this_hour++){
for(this_section=0;this_section<8;this_section++)
{
buff2[this_hour*9+this_section+2]=Schedules[this_hour][this_section];
}
}
for(i=0;i<219;i++){

if(i!=0 && i%9==0){
printf("\n");
}
printf("%d",buff2[i]);
}





return 0;
}

关于c - 数组赋值有奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com