0

For an assignment, we are meant to be able to create a magic square when passed an odd number for the order. However, I can't get my print function to properly display the values that have been set to the array (in the default case, all values are set to zero). Some of the values display accurately while others are showing garbage values.

static const int NUM_ROWS{15}, NUM_COLUMNS{15};

typedef int TwoDimArray[NUM_ROWS][NUM_COLUMNS];



TwoDimArray** buildMagicSquare1(int order) {
    int** table{};
    table = new int *[order];
    for (int row{0}; row < order; ++row)
    {
        table[row] = new int[order];
        for (int col{0}; col < order; ++col)
        {
            table[row][col] = 0;
        }
    }
    // table is now a TwoDimArray initialized to all zeros.
    return reinterpret_cast<TwoDimArray **>(table);
}

void printTable(int rows, int cols, TwoDimArray aTable){
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            std::cout << std::setw(5) << aTable[i][j];
        std::cout << std::endl;
    }
}

int main()
{
    printTable(3, 3, **buildMagicSquare1(3));
}

MoonRider
  • 37
  • 1
  • 8
  • 1
    What is `TwoDimArray` – drescherjm Mar 07 '20 at 01:29
  • `**buildMagicSquare1(3)` looks very suspicious – drescherjm Mar 07 '20 at 01:30
  • I am not sure how a `TwoDimArray**` is convertible to a `TwoDimArray` – drescherjm Mar 07 '20 at 01:35
  • `TwoDimArray` is very different from `int**` – drescherjm Mar 07 '20 at 01:38
  • buildMagicSquare1 function was already given. I did printTable function and the main function. – MoonRider Mar 07 '20 at 01:38
  • What do these "garbage values" look like? For debugging questions, it's usually a good idea to include the actual and expected output. – JaMiT Mar 07 '20 at 02:26
  • Does this answer your question? [conversion of 2D array to pointer-to-pointer](https://stackoverflow.com/questions/8203700/conversion-of-2d-array-to-pointer-to-pointer) – drescherjm Mar 07 '20 at 02:33
  • `typedef int TwoDimArray[NUM_ROWS][NUM_COLUMNS];` makes `TwoDimArray` a `typedef` to type `int (*)[NUM_COLUMNS]` it has nothing to do with with a *pointer-to-pointer*. Why? it is a ***pointer-to-array***. Further, your function type of `TwoDimArray**` would be a *pointer-to-pointer-to-pointer-to-array-of int [15]* -- which begs the question [Is it a good idea to **typedef** pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers).. (or 2D arrays in your case...) – David C. Rankin Mar 07 '20 at 08:15
  • @David: No, wrong, thy typedef in C++ creates the type ````int[15][15]````. If you pass this to a function in a incorrect way, then you have a decay to point problem. And printTable has this wrong argument passing mechanism. The reason, why it is not working, has been given in the comments above. The output is mixed address-data / value. – A M Mar 07 '20 at 11:37
  • Sorry, but I am still really new to c++ so I am still having trouble understanding how to fix this. Sounds like the printTable is to blame here or the way I am passing the table? I unfortunately can't change the buildMagicSquare1 function as that has been given to us. – MoonRider Mar 07 '20 at 19:01
  • @ArminMontigny you are correct, the typedef itself is to `int [15][15]`, but on access (any access other than `sizeof`, `_Alignof`, of when the address is requested with the unary `&` operator), the array is converted to a pointer to its first element - the first element being an array of `int [15]`, thus, unless one of the 3 exceptions apply (they don't here), the type on access is `int (*){15]`. – David C. Rankin Mar 08 '20 at 00:43

0 Answers0