C++ 带有cin实现问题的指针
我是CPP的新人。我试图使用C++ 带有cin实现问题的指针,c++,C++,我是CPP的新人。我试图使用指针和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; in
指针
和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 :(";
}
int*数组;
int numOfElem=0;
库特>努莫菲伦;
数组=新的(nothrow)整数[numOfElem];
如果(数组!=0)
{
for(int index=0;index cout循环中的array++
会增加指针,因此在完成第一个循环时,array
将指向最初分配的数组之外
照办
cin >> *(array+index);
或者干脆
cin >> array[index];
循环中的array++
会增加指针,因此在完成第一个循环时,array
将指向最初分配的数组之外
照办
cin >> *(array+index);
或者干脆
cin >> array[index];
您正在第一个循环中前进指针数组
:
for(int index = 0; index < numOfElem; index++)
{
cout << "\nEnter " << index << " value";
cin >> *array++;
}
for(int-index=0;index cout您正在推进第一个循环中的指针,array
:
for(int index = 0; index < numOfElem; index++)
{
cout << "\nEnter " << index << " value";
cin >> *array++;
}
for(int-index=0;index 非常感谢。有什么方法可以使指针在第一个循环后再次指向第一个位置吗?从中减去numOfElem
,因为您正在向它添加numOfElem
。非常感谢。有什么方法可以使指针在第一个循环后再次指向第一个位置吗?减去numOfElemnumOfElem
one。