#include <iostream>
using namespace std;
int main()
{
int* p;
int arr[10];
p = arr;
for( int i = 0; i <10; i++)
{
//arr[i] = i; //这句可以得到预计的结果
//*(p + i) = i //这句也可以
*(p++) = i; //这句为什么不行
}
for(int i = 0; i < 10; i++)
{
cout << *(p++) <<endl; //这句可以`请输入代码`
}
}
高洛峰2017-04-17 13:18:27
When your first for loop ends, p already points to the last element of the array. This is completely out of bounds when using cout.