gpt4 book ai didi

c - C中的指针递增

转载 作者:太空狗 更新时间:2023-10-29 15:26:42 25 4
gpt4 key购买 nike

考虑以下代码:

unsigned short i;
unsigned short a;
unsigned char *pInput = (unsigned char *)&i;

pInput[0] = 0xDE;
pInput[1] = 0x01;

a = ((unsigned short)(*pInput++)) << 8 | ((unsigned short)(*pInput++));

为什么a的值为0xDEDE,而不是0xDE01?

最佳答案

代码调用了未定义的行为。原因是 pInput 在两个 sequence points 之间修改了不止一次.您可能会得到任何预期或意外的结果。无话可说。

C99 规定:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

阅读c-faq 3.8以获得更详细的解释。

关于c - C中的指针递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22455770/

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