Creating a 2D array inside a struct

Been trying different ways and researching this but anything I make keeps failing with this idea.

What I want to do is along the lines of:
1
2
3
4
5
6
7
8
9
10
struct myStruct
{
    char symbol[4][4];
};

myStruct sItem;

sItem.symbol[4][4] = {'x', 'x', 'x',
                      'x', 'x', 'x',
                      'x', 'x', 'x'};


I know this isn't correct and not sure if it can even be done?

But to have to so the output would be:

xxx
xxx
xxx

Hi Mythios,

There is one simple problem, it's that you are trying to initialize the value to an array in
1
2
3
sItem.symbol[4][4] = {'x', 'x', 'x',
                      'x', 'x', 'x',
                      'x', 'x', 'x'};



Try it this way

1
2
3
4
5
6
7
8
9
10
11
12
13
14

for (int i=0;i<3;i++) {
    for (int j=0;j<3;j++) { 
           sItem.symbol[i][j] = 'x';
    } 

}

for (int i=0;i<3;i++) { 
    for (int j=0;j<3;j++) {
          cout<<sItem.symbol[i][j]<<"  "; 
   }
cout<<endl;
}


Hope this helps !
I understand that and i had it working like that fine. But lets say i dont want them all x's i might want xox, xox, xox etc. That way wont help me then will it?

Just as I'm using it to draw a small face etc.
Sorry for misunderstanding.

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct myStruct
{
    char (*symbol)[3];
    myStruct(char temp[3][3]) : symbol(temp) { } 
    myStruct() {} 
};

int main() { 

char c[3][3] = {{'x','o','x'},{'x','o','x'},{'x','o','x'}}; 
myStruct obj(c); 

for (int i=0;i<3;i++) { 
    for (int j=0;j<3;j++) {
          cout<<obj.symbol[i][j]<<"  "; 
   }
cout<<endl;
}

return 0;

} 


Hope this helps !
Thanks for the help you've given but sadly it doesn't cover the problem. As you've created a char c[3][3]; which could be done a lot easier than above. Like my example at the top it just pulls out the symbol char member right away and allowed it to be edited as soon as its used. I know this might not even be possible? But yeah sorry not exactly what I needed:)

Thanks,
Myth.
Topic archived. No new replies allowed.