我是CPP的新手。我试图使用pointer
和cin
组合,这给了奇怪的结果。cin执行问题的指针
int *array;
int numOfElem = 0;
cout << "\nEnter number of elements in array : ";
cin >> numOfElem;
array = new (nothrow)int[numOfElem];
if(array != 0)
{
for(int index = 0; index < numOfElem; index++)
{
cout << "\nEnter " << index << " value";
cin >> *array++;
}
cout << "\n values are : " ;
for(int index = 0; index < numOfElem; index++)
{
cout << *(array+index) << ",";
}
}else
{
cout << "Memory cant be allocated :(";
}
的出看跌期权
什么我的代码的问题?
问候,
沙
非常感谢。有什么方法可以使指针在第一个循环后再次指向第一个位置? – sha 2013-02-17 10:19:42
减去'numOfElem',因为你正在向它添加'numOfElem'。 – 2013-02-17 10:21:05