#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の値を変更するIは*p
のprintf()
を0x7ffc000f47c8にあるものの値ではなく、ポインタの符号なしの値であることが期待されますp
(956816552))が増分ポインタをDeferencingた(* p ++ = X)P
私は*p
の値を956816552(p
の値)に設定しましたか?
私は*p++ = 4
がUBではないと信じています。 (1番目の回答のコメントによると - Undefined behavior and sequence points)
何か助けに感謝します。ありがとう。
*私は* p ++ = 4がUBではないと信じているかもしれませんが、おそらくそうではないかもしれませんが、実際にはあなたが所有していないメモリを指しています。 – stijn
C++の行動規範: – CinCout
おそらく 'p'はメモリ内の' i'に従うので、 'p ++'はそれ自身を指し示す' p'を残します。あなたは確かに "0x7ffc000f47c8にあるもの"を見ています –