initialize char matrix

Hello to everybody!
I have a global variable initialized like that:
char matrix[][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

The matrix is modified during the program and I would to write a function that redo the initialization up.
If I write simply matrix = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; returns the error "assigning to an array from an initializer list"

Where's the problem?

thanks to everybody
The problem is that initializer lists are only valid in initialization statements.
You could simply do
1
2
3
4
5
for (int i = 0; i < 9; i++){
    //Watch out! You might need to flip around the indices.
    //I can never remember which is which.
    matrix[i / 3][i % 3] = '0' + i;
}
Using std::array<> to wrap the c-style array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <array>

template < typename MATRIX > void print( const MATRIX& matrix )
{
    for( const auto& row : matrix )
    {
        for( const auto& v : row ) std::cout << v << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "-------\n" ;
}

int main()
{
    std::array< std::array< char, 3 >, 3 > matrix = { { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} } };
    print(matrix) ;

    matrix = { { {'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'} } } ;
    print(matrix) ;

    matrix = { { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} } } ;
    print(matrix) ;
}

http://coliru.stacked-crooked.com/a/f49ef6047af22749
Last edited on
Topic archived. No new replies allowed.