1

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++;
                }
            }
        }
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Noah
  • 178
  • 1
  • 7
  • 3
    Have you thought about using `std::vectors` – Ed Heal Dec 11 '15 at 23:14
  • What you are showing us appears correct. Show more. – Mike Nakis Dec 11 '15 at 23:20
  • For example: how do you initialize these int arrays? And how do you determine whether the values are not updated? – Mike Nakis Dec 11 '15 at 23:21
  • You're overwriting the same `vacancies[i]` each time through the inner loop. – Barmar Dec 11 '15 at 23:27
  • So all `vacancies[i]` will point to the last `0` element in `grid`. – Barmar Dec 11 '15 at 23:28
  • Oh, Barmar, you are right. I have updated the code above, although it did not change the output. – Noah Dec 11 '15 at 23:35
  • Please don't change the question. Now my answer makes no sense, since readers can't see the original question that I was correcting. – Barmar Dec 11 '15 at 23:37
  • Aren't you getting a compiler warning about trying to use the value of a void function? – Barmar Dec 11 '15 at 23:37
  • `getVacancies(grid)` is wrong -- `getVacancies()` takes 3 arguments. And you can't use it in the assignment to `vacancies`, because `vacancies` is one of the arguments. – Barmar Dec 11 '15 at 23:39
  • Did you mean to call a different function, that counts the number of vacancies in `grid`? – Barmar Dec 11 '15 at 23:39
  • My apologies, I have reverted it, and added in your revision as a new edit. – Noah Dec 11 '15 at 23:40
  • Correct, I have overloaded getVacancies with 2 functions, the one you see above and one which simply counts the number of vacancies in grid. – Noah Dec 11 '15 at 23:40

3 Answers3

1

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.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
1

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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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];
}
Community
  • 1
  • 1
Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • 1
    He's edited the question to show how he initializes the arrays. This isn't what he needs. – Barmar Dec 11 '15 at 23:29