Question

I'm trying to turn a 2 dimensional array into a 1 dimensional array in C++ using a pointer to return the new 1 dimensional array back to the main() I've tried a few different things but I keep getting syntax errors. The new array should be just numbers 1-9 this is a summery of what I got going on.

char changearray(char* matrix[3][3])
{
    char* newarray[9];
    newarray[0] = matrix[0][0];
    newarray[1] = matrix[0][1];
    newarray[2] = matrix[0][2];
    newarray[3] = matrix[1][0];
    newarray[4] = matrix[1][1];
    newarray[5] = matrix[1][2];
    newarray[6] = matrix[2][0];
    newarray[7] = matrix[2][1];
    newarray[8] = matrix[2][2];
    return *newarray;

}
int main()
{
   char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

   char *boardptr = Board;
   changearray(Board);
}
Was it helpful?

Solution 2

Another input is needed as a buffer to store the output, newarray is local and will be erased when the function ends.

void changearray(char matrix[3][3], char Array[9])
{
    Array[0] = matrix[0][0];
    Array[1] = matrix[0][1];
    Array[2] = matrix[0][2];
    Array[3] = matrix[1][0];
    Array[4] = matrix[1][1];
    Array[5] = matrix[1][2];
    Array[6] = matrix[2][0];
    Array[7] = matrix[2][1];
    Array[8] = matrix[2][2];
    //for( int i = 0; i < 9; i++ )
    //    Array[i] = matrix[i/3][i%3];
}
int main()
{
    char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

    char Array[9];
    changearray(Board, Array);
}

The other option is to procedurally allocate memory with the new keyword.

char* changearray(char matrix[3][3])
{
    char* Array = new char[9]; // Allocate Memory
    Array[0] = matrix[0][0];
    Array[1] = matrix[0][1];
    Array[2] = matrix[0][2];
    Array[3] = matrix[1][0];
    Array[4] = matrix[1][1];
    Array[5] = matrix[1][2];
    Array[6] = matrix[2][0];
    Array[7] = matrix[2][1];
    Array[8] = matrix[2][2];
    return Array;
}
int main()
{
   char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

   char* Array = changearray(Board);

   // Use Array

   delete[] Array; // Release data
}

Note: C++ treats two dimensional arrays and one dimensional arrays semi-identically, more like an array of arrays. You can use Board[0][5] or (*Board)[5] to indicate Board[1][2] instead.

OTHER TIPS

Your array is statically allocated thus already is a 1D dimensional array in memory. A function isn't needed. This will work:

int main()
{
   char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

   char *boardptr = &Board[0][0];
}

boardptr is now a 1D array with a layout identical to what your function would have produced.

Try this:

char* changearray(char* matrix[3][3])
{
    char* newarray = new char[9];
    newarray[0] = matrix[0][0];
    newarray[1] = matrix[0][1];
    newarray[2] = matrix[0][2];
    newarray[3] = matrix[1][0];
    newarray[4] = matrix[1][1];
    newarray[5] = matrix[1][2];
    newarray[6] = matrix[2][0];
    newarray[7] = matrix[2][1];
    newarray[8] = matrix[2][2];
    return newarray;
}

int main()
{
   char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

   char *boardptr = changearray(Board);
}

For what it's worth, I would propose changing the structure of your program to encapsulate this view into some sort of interface. Of course, I don't know the context of your application and so I can't do anything but to provide you another option to choose from.

The code I'm providing here takes a pointer to your array and allows you to access elements via offset and x/y coords.

#include <cctype>

template <typename T, size_t rows, size_t cols>
class MatrixView {
public:
    MatrixView(T (*matrix)[rows][cols]) : _matrix(matrix) {}

    T& at(size_t x, size_t y) { return *_matrix[y][x]; }
    T& at(size_t offset) { return *_matrix[offset % cols][offset / cols]; }

private:
    T (*_matrix)[rows][cols];
};

#include <cassert>

int main()
{
    char Board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

    MatrixView<char, 3, 3> matrixView(&Board);

    assert(matrixView.at(0, 0) == '1');
    assert(matrixView.at(0, 1) == '2');
    assert(matrixView.at(0, 2) == '3');
    assert(matrixView.at(1, 0) == '4');
    assert(matrixView.at(1, 1) == '5');
    assert(matrixView.at(1, 2) == '6');
    assert(matrixView.at(2, 0) == '7');
    assert(matrixView.at(2, 1) == '8');
    assert(matrixView.at(2, 2) == '9');

    assert(matrixView.at(0) == '1');
    assert(matrixView.at(1) == '2');
    assert(matrixView.at(2) == '3');
    assert(matrixView.at(3) == '4');
    assert(matrixView.at(4) == '5');
    assert(matrixView.at(5) == '6');
    assert(matrixView.at(6) == '7');
    assert(matrixView.at(7) == '8');
    assert(matrixView.at(8) == '9');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top