简体   繁体   中英

Dereferencing Pointer to Pointer

I'm trying to run through a 2-dimensional array and update values using a pointer to pointer to 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.

Edit 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. 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). Instead, you just want to loop through grid , and add each vacant element to the next entry in 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.

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? ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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