我在使用指针时遇到了这么困难。基本上,我想要一个指向无符号字符kbdbuff []的buff void指针,这样每当kbdbuff []发生更改时,它都会反映在buff指向的值中。 我最接近的是这样做:
kbdbuff[0] = 4;
unsigned char *tempbuf = buff;
*tempbuf = *kbdbuff;
Buff指针最后指向4,但问题是指定指针后所做的任何更改都不会反映在buff指向的值中。任何帮助将不胜感激。
答案 0 :(得分:1)
int main(void)
{
void* buff;
unsigned int kbdbuff[4];
buff = kbdbuff;
kbdbuff[0] = 4;
printf("kbdbuff[0] = %d\n", kbdbuff[0]);
printf("buff[0] = %d\n", ((int*)buff)[0]);
kbdbuff[0] = 3;
printf("kbdbuff[0] = %d\n", kbdbuff[0]);
printf("buff[0] = %d\n", ((int*)buff)[0]);
return 0;
}
输出:
kbdbuff[0] = 4
buff[0] = 4
kbdbuff[0] = 3
buff[0] = 3