Returning a 2d array of constant values.

Hi!

How can I return a 2d array of constant values from a function?
I need to return something like this:
1
2
3
4
5
6
7
//I don't even know if this is the best way to do 2d arrays in c++.
const int m[4][2] = {
	{10,11},
	{12,13},
	{14,15},
	{16,17}
};


I tried this but did not work.
1
2
3
4
5
6
7
8
9
10
const int** createMatrix(){
	const int **matrix = new const int*[4];

	matrix[0] = new const int[2] = {10,11};
	matrix[1] = new const int[2] = {12,13};
	matrix[2] = new const int[2] = {14,15};
	matrix[3] = new const int[2] = {16,17};

	return matrix;
}
Last edited on
You can actually return a reference to an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef const int(&ref_matrix)[4][2];

ref_matrix createMatrix(){
static const int m[4][2] = {
	{10,11},
	{12,13},
	{14,15},
	{16,17}
};

	return m;
}

int main()
{
    ref_matrix x = createMatrix();

    return 0;
}


But why can't you use m directly? What are you trying to do?
I tried this but did not work.

Please describe the failure.
Here the entire code and the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int** createMatrix();

int main(){
	int **m = createMatrix();
	return 0;
}

const int** createMatrix(){
	const int **matrix = new const int*[4];

	matrix[0] = new const int[2] = {10,11};
	matrix[1] = new const int[2] = {12,13};
	matrix[2] = new const int[2] = {14,15};
	matrix[3] = new const int[2] = {16,17};

	return matrix;
}

main.cpp: In function ‘const int** createMatrix()’:
main.cpp:12:13: error: ambiguating new declaration of ‘const int** createMatrix()’
const int** createMatrix(){
^~~~~~~~~~~~
main.cpp:5:7: note: old declaration ‘int** createMatrix()’
int** createMatrix();
^~~~~~~~~~~~
main.cpp:15:29: error: uninitialized const in ‘new’ of ‘const int’
matrix[0] = new const int[2] = {10,11};
^
main.cpp:16:29: error: uninitialized const in ‘new’ of ‘const int’
matrix[1] = new const int[2] = {12,13};
^
main.cpp:17:29: error: uninitialized const in ‘new’ of ‘const int’
matrix[2] = new const int[2] = {14,15};
^
main.cpp:18:29: error: uninitialized const in ‘new’ of ‘const int’
matrix[3] = new const int[2] = {16,17};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <array>
#include <vector>

// http://en.cppreference.com/w/cpp/language/namespace
namespace static_sz {

    const std::size_t NCOLS = 2 ;
    const std::size_t NROWS = 4 ;

    // http://en.cppreference.com/w/cpp/container/array
    // http://en.cppreference.com/w/cpp/language/type_alias
    using row_type = std::array< int, NCOLS > ;
    using matrix_type = std::array< row_type, NROWS > ;

    matrix_type createMatrix();
}

namespace dynamic_sz {

    // https://cal-linux.com/tutorials/vectors.html
    using row_type = std::vector<int> ;
    using matrix_type = std::vector<row_type> ;

    matrix_type createMatrix( std::size_t nrows, std::size_t ncols );
}

int main() {

    {
        // http://www.stroustrup.com/C++11FAQ.html#auto
        const auto mtx = static_sz::createMatrix();

        // http://www.stroustrup.com/C++11FAQ.html#for
        for( const auto& row : mtx ) {

            for( int v : row ) std::cout << v << ' ' ;
            std::cout << '\n' ;
        }
    }

    {
        std::size_t nrows = 0 ;
        std::cout << "\n#rows? " && std::cin >> nrows ;

        std::size_t ncols = 0 ;
        std::cout << "#cols? " && std::cin >> ncols ;

        std::cout << "\ncreate matrix " << nrows << 'x' << ncols << '\n' ;

        const auto mtx = dynamic_sz::createMatrix( nrows, ncols );

        for( const auto& row : mtx ) {

            for( int v : row ) std::cout << v << ' ' ;
            std::cout << '\n' ;
        }
    }
}

static_sz::matrix_type static_sz::createMatrix() {

	return {{ {10,11}, {12,13}, {14,15}, {16,17} }} ;
}

dynamic_sz::matrix_type dynamic_sz::createMatrix( std::size_t nrows, std::size_t ncols ) {

	int v = 10 ;

	matrix_type mtx ;

    while( mtx.size() < nrows ) {

	    row_type row ;
	    //http://en.cppreference.com/w/cpp/container/vector/push_back
        while( row.size() < ncols ) row.push_back( v++ ) ;

        mtx.push_back(row) ;
    }

    return mtx ;
}

http://coliru.stacked-crooked.com/a/dd50b4e251434aca
@robgeek:
Your attempt had two issues:

1. Compare lines 5 and 12.
First function returns int*, but the second const int*.
That is a mismatch. Lines 5 and 8 need the const too.

2. Initialization syntax. See http://www.informit.com/articles/article.aspx?p=1852519
* C++03 could not initialize a dynamically allocated POD array
* C++11 did add brace initialization syntax. That does not have '='.

matrix[0] = new const int[2] {10,11};


While we can fix it, you should not desire to juggle with raw pointers.

JLBorges shows above much safer matrix types. Yes, real types instead of a pointer that you have to keep track of meticulously. While it might seem a wall of text for one matrix, it can serve you in thousand matrices with no additional effort.
Topic archived. No new replies allowed.