我对打印指针值和数组有疑问。
int arr[5] = { 1, 2, 3, 4, 5 };
int * ptr = arr;
for (int i = 0; i < 5; i++) {
(*ptr) += 2;
ptr++;
printf("%d", (*ptr));
}
Run Code Online (Sandbox Code Playgroud)
以上是我首先输入的内容,但没有用。所以我擦除了printf行,并输入了一个新的代码。而且有效。
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
Run Code Online (Sandbox Code Playgroud)
我知道第二个为什么起作用,但仍然不明白第一个为什么不能起作用。
预期输出为3 4 5 6 7,但第一个代码的实际输出为2 3 4 5 -858993460
c ×1