typedef struct { unsigned short *Buf; unsigned short *PrtbufVal; unsigned char BufLen; } AdcTypeS; int main(void) { static unsigned char temp = 0; static unsigned char t = 0; static unsigned char i = 0; unsigned long a[56] = {1,2,3,4,5,6,7.8,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28.29,30,31,32,33,34,35,36,37,38,39,40,41,46,43,44,45,46,47,48,49,50,51,52,53,54,55}; unsigned long *p; p = a; unsigned short aBuf[10]; unsigned short bBuf[11]; unsigned short cBuf[12]; unsigned short aBufcn = 0; unsigned short bBufCnt = 0; unsigned short cBufCnt = 0; AdcTypeS adc[3] = { {aBuf, &aBufcn, 10}, {bBuf, &bBufCnt, 11}, {cBuf, &cBufCnt, 12} }; while (++t <= 56) { if ((adc[temp].PrtbufVal)++ >= adc[temp].BufLen) { *adc[temp].PrtbufVal = 0; } adc[temp].Buf[*adc[temp].PrtbufVal] = (unsigned short)p[i]; i++; if (++temp >= 3) { temp = 0; } printf("%d, %lu, %d, %lu\n", temp, *adc[temp].PrtbufVal, adc[temp].Buf[adc[temp].PrtbufVal], p[i]); } return 0; } 这段代码,当
时间: 2023-09-24 10:12:07 浏览: 96
这段代码执行时,会依次将数组a的元素赋值给adc数组中的Buf,并且循环打印出temp、*adc[temp].PrtbufVal、adc[temp].Buf[adc[temp].PrtbufVal]和p[i]的值。根据代码的逻辑,temp的值会在0、1、2之间循环,i的值会从0递增到55,所以总共会执行56次循环。在每次循环中,会根据temp的值选择不同的adc元素进行操作。具体的操作为,将p[i]的值赋给adc[temp].Buf[*adc[temp].PrtbufVal],然后增加*adc[temp].PrtbufVal的值,如果超过了adc[temp].BufLen的大小,则将*adc[temp].PrtbufVal的值重置为0。
最后,会打印出temp、*adc[temp].PrtbufVal、adc[temp].Buf[adc[temp].PrtbufVal]和p[i]的值。
相关问题
typedef 'unsigned int*' but the agrument has type 'unsigned char *'
这个错误提示意味着你正在使用一个指向 `unsigned int` 类型的指针,但是你传递给函数的参数类型却是 `unsigned char` 类型的指针。
可能的原因是你的函数参数类型声明不正确,或者你在调用函数时传递了错误的参数类型。请检查你的代码,并确保你的函数参数类型与你的函数调用的参数类型匹配。
typedef struct { unsigned short *Buf; unsigned short *PrtbufVal; unsigned char BufLen; } AdcTypeS; int main(void) { static unsigned char temp = 0; static unsigned char t = 0; static unsigned char i = 0; unsigned long a[56] = {1,2,3,4,5,6,7.8,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28.29,30,31,32,33,34,35,36,37,38,39,40,41,46,43,44,45,46,47,48,49,50,51,52,53,54,55}; unsigned long *p; p = a; unsigned short aBuf[10]; unsigned short bBuf[11]; unsigned short cBuf[12]; unsigned short aBufcn = 0; unsigned short bBufCnt = 0; unsigned short cBufCnt = 0; AdcTypeS adc[3] = { {aBuf, &aBufcn, 10}, {bBuf, &bBufCnt, 11}, {cBuf, &cBufCnt, 12} }; while (++t <= 56) { if ((*adc[temp].PrtbufVal)++ >= adc[temp].BufLen) { *adc[temp].PrtbufVal = 0; } adc[temp].Buf[*adc[temp].PrtbufVal] = (unsigned short)p[i]; i++; if (++temp >= 3) { temp = 0; } printf("%d, %lu, %d, %lu\n", temp, *adc[temp].PrtbufVal, adc[temp].Buf[*adc[temp].PrtbufVal], p[i]); } return 0; } }当*adc[temp].PrtbufVal为0时,adc[temp].Buf[adc[temp].PrtbufVal]的值是不对,是随机数,为什么,怎么改
当`*adc[temp].PrtbufVal`为0时`adc[temp].Buf[adc[temp].rtbufVal]`的值是随机数的原因是因为在赋值之前,先对`*adc[temp].PrtbufVal`进行了自增操作,导致索引值不正确。
要修复这个问题,你可以将自增操作移动到赋值操作之后。修改代码如下:
```c
if (*adc[temp].PrtbufVal >= adc[temp].BufLen) {
*adc[temp].PrtbufVal = 0;
}
adc[temp].Buf[*adc[temp].PrtbufVal] = (unsigned short)p[i];
(*adc[temp].PrtbufVal)++;
```
这样就保证了先进行赋值操作,然后再自增`*adc[temp].PrtbufVal`的值。这样可以避免在索引为0时出现随机数的问题。
阅读全文