Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带有cin实现问题的指针_C++ - Fatal编程技术网

C++ 带有cin实现问题的指针

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

我是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; 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;indexcout循环中的
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;indexcout您正在推进第一个循环中的指针,
array

for(int index = 0; index < numOfElem; index++)
{
    cout << "\nEnter " << index << " value";
    cin >> *array++;
}
for(int-index=0;index非常感谢。有什么方法可以使指针在第一个循环后再次指向第一个位置吗?从中减去
numOfElem
,因为您正在向它添加
numOfElem
。非常感谢。有什么方法可以使指针在第一个循环后再次指向第一个位置吗?减去
numOfElemnumOfElem
one。