我正在尝试使用指针循环遍历整数数组,但我得到了一些奇怪的值。
当我运行时,我会得到以下输出:
为什么我得到了这些奇怪的值?我应该只看到“1”,因为我循环直到获取NULL指针,每次循环我都移动下一个指针。
int nums[1] = { 1 };
int *p = nums;
while(*p != NULL) {
cout << " LOOPING, p is " << *p << endl;
p++;
}
当我运行时,我会得到以下输出:
LOOPING, p is 1
LOOPING, p is -858993460
LOOPING, p is 4454504
LOOPING, p is 3032019
为什么我得到了这些奇怪的值?我应该只看到“1”,因为我循环直到获取NULL指针,每次循环我都移动下一个指针。