我很好奇为什么在这段代码中 *(ptr + 1) 的值在程序运行时变为 1 而不是 6。
#include <stdio.h>
int main (void)
{
int *ptr, content = 3;
ptr = &content;
*(ptr + 1) = 6;
*(ptr + 2) = 10;
*(ptr + 3) = 14;
int i = 0;
for (i = 0; i < 4; ++i)
{
printf("The address is %p and the content is %d \n", ptr+i, *(ptr+i));
}
return 0;
}
运行时 int 6 被修改为值 1。
*(ptr + 1) = 6;
这是程序的输出:
The address is 0x7fff7b400a88 and the content is 3
The address is 0x7fff7b400a8c and the content is 1
The address is 0x7fff7b400a90 and the content is 10
The address is 0x7fff7b400a94 and the content is 14
我在 valgrind 中运行它并且没有出现任何错误或任何东西,还尝试使用谷歌搜索它,也许我正在寻找错误的东西但没有运气找到结果。