Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

int x = -2;
int y = 0;
memcpy(&y,&x,2);
memcpy(&y+2,&x+2,2);
此时y的值为什么不是-2呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

memcpy(&y,&x,sizeof(int));

sizeof(int) 不一定是 2

&x+2 不是 &x 之后两个字节,而是 &x 之后两个 int (两个 sizeof(int) 字节)。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...