简体   繁体   English

将指针解引用到指针

[英]Dereferencing Pointer to Pointer

I'm trying to run through a 2-dimensional array and update values using a pointer to pointer to int. 我正在尝试遍历二维数组并使用指向int的指针更新值。

Swap function: 交换功能:

void foo(int ** vacancies, int **transfers, int transfer)
{
    for (int i = 0; i < transfer; i++)
    {
        (*transfers[i]) = 0;
        (*vacancies[i]) = 2;
    }
}

Declaration: 宣言:

int ** vacancies = new int*[getVacancies(grid)];
int ** transfers = new int*[transfer];

Function call: 函数调用:

foo(vacancies, transfers, transfer);

Unfortunately this doesn't seem to actually update any values, is there something I need to change? 不幸的是,这似乎并没有真正更新任何值,我是否需要更改? Thanks! 谢谢!

Edit: 编辑:

getVacancies(vacancies, grid, transfer);
getTransfers(transfers, grid, transfer);

    void getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
    for (int i = 0; i < vCount; i++)
    {
        for (int row = 0; row < ROW; row++)
        {
            for (int col = 0; col < COL; col++)
            {
                if (grid[col][row] == 0)
                {
                    vacancies[i] = &grid[col][row];
                }

            }
        }
    }
}

And the same for getTransfers. 与getTransfers相同。

Edit 2: 编辑2:

void getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
    int i = 0;
        for (int row = 0; row < ROW; row++)
        {
            for (int col = 0; col < COL; col++)
            {
                if (grid[col][row] == 0)
                {
                    vacancies[i] = &grid[col][row];
                    i++;
                }
            }
        }
}

You have allocated only "one dimension". 您仅分配了“一个维度”。 Those int* elements should point to arrays of int or just int s. 这些int*元素应指向intint数组。 Dereferencing these uninitialized pointers is undefined behavior. 取消引用这些未初始化的指针是未定义的行为。

I think this is how you need to initialize your array. 我认为这就是您需要初始化数组的方式。 You shouldn't loop through vacancies , because that will fill each element with a pointer to the same element of grid (the last vacant one). 您不应遍历vacancies ,因为那样会用指向grid同一元素(最后一个空缺的一个)的指针填充每个元素。 Instead, you just want to loop through grid , and add each vacant element to the next entry in vacancies . 相反,您只想遍历grid并将每个空元素添加到vacancies的下一个条目。

I've also changed the function to return the number of elements that were filled in. Alternatively, you could initialize vacancies to nullptr in each element, and test for this when looping through it later. 我还更改了该函数以返回已填充的元素数。或者,您可以在每个元素中将vacancies初始化为nullptr ,并在以后遍历该元素时对其进行测试。

int getVacancies(int ** vacancies, int grid[][ROW], int vCount)
{
    int i = 0;
    for (int row = 0; row < ROW; row++)
    {
        for (int col = 0; col < COL; col++)
        {
            if (grid[col][row] == 0)
            {
                if (i >= vCount) { // Prevent overflowing vacancies
                    return i;
                }
                vacancies[i++] = &grid[col][row];
            }
        }
    }
    return i;
}

Allocate a two dimensional array like this (see How do I declare a 2d array in C++ using new? ): 像这样分配一个二维数组(请参阅如何使用new在C ++中声明2d数组? ):

int** twoDimensionalArray = new int*[rowCount];
for (int i = 0; i < rowCount; ++i) {
    twoDimensionalArray[i] = new int[colCount];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM