Noah
Noah

Reputation: 178

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++;
                }
            }
        }
}

Upvotes: 1

Views: 134

Answers (3)

Barmar
Barmar

Reputation: 780724

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;
}

Upvotes: 1

Andreas Vogl
Andreas Vogl

Reputation: 1864

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];
}

Upvotes: 0

LogicStuff
LogicStuff

Reputation: 19607

You have allocated only "one dimension". Those int* elements should point to arrays of int or just ints. Dereferencing these uninitialized pointers is undefined behavior.

Upvotes: 1

Related Questions