#include <stdio.h>
int main(){
int i=1;
int * p = &i;
*(p++)=4;
printf("%p\n", p); //0x7ffc000f47c8
printf("%u\n", p); //956816552 (which is 0x7ffc000f47c8 as unsigned integer)
printf("%u\n", *p); //956816552 (I would expect *p to be the value of whatever is in 0x7ffc000f47c8, and not the unsigned value of the pointer p (956816552))
return 0;
}
我期望的*p
的printf()
要的無論是在0x7ffc000f47c8的值,而不是指針的無符號值的值p
(956816552))Deferencing指針與增量(* P ++ = x)的變化* P的至p
何時/如何將*p
的值設置爲956816552(值爲p
)?我認爲*p++ = 4
不是UB。 (根據第一個回答的評論 - Undefined behavior and sequence points)
任何幫助將不勝感激。謝謝。
*我相信* p ++ = 4不是UB *可能不是,但是在解除引用p之後,它現在指向您並不真正擁有的內存 – stijn
C++行爲準則:只有在您知道所有者時才解除引用。 – CinCout
可能'p'恰好在內存中跟隨'i',所以'p ++'離開'p'指向自己。你確實看到「無論在0x7ffc000f47c8」 –