tags:

views:

98

answers:

3

I define a 2D array in my header file

char map[3][3];

How can I initialize the values in the class constructor like this

 map = {{'x', 'x', 'o'},
       {'o', 'o', 'x'},
       {'x', 'o', 'x'}};
A: 

yes you can do just as you demonstrated in your code.

C Johnson
I have tried it. I get a 'expression must be a modifiable lvalue' error.
Arizona1911
There are some limitations in using array initializers.
rwong
@rwong: I don't understand. Why does it fail?
JoshD
A: 
memcpy(map,"xxoooxxox",9);

or

char tmp[3][3] =
    {{'x','x','o'},
     {'o','o','x'},
     {'x','o','x'}};
memcpy(map,tmp,9);
PigBen
`char tmp[] = "xxoooxxox"; char* map[3] = { tmp + 0, tmp + 3, tmp + 6 }; `
rwong
@rwong as soon as the tmp array is destroyed your map will have invalid pointers.
Michael Anderson
@rwong You just created a new array. This will have no effect on the array that's part of his class.
PigBen
@Michael: my answer was responding to the earlier version of PigBen's answer, which has been edited away. What I wanted to point out is that taking the address of `tmp` from `char tmp[3][3]` would not give a pointer to `char`, it would give a pointer *to pointer* to `char`.
rwong
Sorry, I didn't read question careful enough. None of the code works. [Chubsdad](http://stackoverflow.com/questions/3878670#3878862) had given the answer.
rwong
@rwong Yes, it would be a pointer to a pointer, but it would still work. Since that address is the address of the first char of the first array.
PigBen
+1  A: 

Firstly, there is a difference between assignment and initialization. The OP title is about initialization.

Secondly, you have not told us if your 2D array is a class member(static/non static) or a namespace variable.

-Since you mentioned about initializing it in the class constructor, I am assuming that it is a class non static member, because:

$12.6.2/2 - "Unless the mem-initializer-id names the constructor’s class, a non-static data member of the constructor’s class, or a direct or virtual base of that class, the mem-initializer is ill-formed."

Further, as of C++03 the member array cannot be initialized in the constructor initializer list for the case in OP(not sure about C++0x) though.

-If your 2D array is a static member of your class, you should initialize it as you did (with a slight change), but not in the constructor. This should be done in the enclosing namespace scope once and only once in any of the translation units.

char (A::map)[3][3] = {{'x', 'x', 'o'}, 
       {'o', 'o', 'x'}, 
       {'x', 'o', 'x'}};

-Alternatively, if your 2D array is a namespace scope variable, the definition of the array should be taken out of the header file (unless it is also static) as it will cause a redefinition error and be defined and initialized once and only once in any translation unit as

char map[3][3] = {{'x', 'x', 'o'}, 
       {'o', 'o', 'x'}, 
       {'x', 'o', 'x'}}; 
Chubsdad
Well, since initializing an array in an initialization list is impossible, it must be done by way of assignment in the body of the constructor. So I would call that "initializing" even if it wasn't done upon creation of the variable.
PigBen
As Scott Myers says, assignment is better than initialization for built in data types. Much easier to mantain and less error prone !!
DumbCoder