c ++与数组的多级间接

时间:2014-09-09 03:34:56

标签: c++ arrays pointers

当= to size时,我无法在arr [0]和arr [1]中显示值。它显示的是地址。我该如何解决这个问题。

int checkVal (int size, int c)
{
    cout << c << ". ";
    cin >> size;
    do{if (size < 0)
    {
            cout << size << " is not a non-negative integer. Re-enter --> " << c << ". ";
        cin >> size;
    }
    }while(size < 0);

    return (size);

}

void Input (int **&x, int **arr, int size1,int size2, int a, int b)
{

    cout << "Please enter 2 non-negative integer values: "<< endl;
    checkVal(size1, a);
    checkVal(size2, b);

    putArr(x,size1,size2);

    arr[0] = size1;
    arr[1] = size2;
    cout << arr[0] << "   " << arr[1] << endl;
}

int main()
{
    int size1, size2;
    int a = 1, b = 2;

    int** x;
    int*** y;
    int** q;
    int**** z;

    int *arr [2];

    allocArr(x, y, q, z);
    Input(x, arr, size1, size2, a, b);
    checkVal(size1,a);
    putArr(x, size1, size2);
    summation(y, arr);
    display(z);


}

是的,所有指针都是这个特定程序所必需的。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

checkVal按值获取size。它不会改变发送给函数的变量。

所以看起来像checkVal需要通过引用获得大小,而且您不需要返回。

像这样:

void checkVal (int &size, int c)
{
    do{
       cout << c << ". ";
       cin >> size;
       if(size < 0){
          cout << size << " is not a non-negative integer. Re-enter --> ";
       }
   }while(size < 0);
}

另外:你确定指针是正确的吗?我看到太多的星星(*)