views:

299

answers:

8

Hey everyone! Could someone please help me understand this error in C for structures? This is my code:

struct Orientation
{
    char facing;
    char sensor;
    char mazeDir;
}; 

struct Orientation O[16];
O[0] = {'N', 'F', 'N'};
O[1] = {'N', 'B', 'S'};
O[2] = {'N', 'R', 'E'};
O[3] = {'N', 'L', 'W'};
O[4] = {'S', 'F', 'S'};
O[5] = {'S', 'B', 'N'};
O[6] = {'S', 'R', 'W'};
O[7] = {'S', 'L', 'E'};
O[8] = {'E', 'F', 'E'};
O[9] = {'E', 'B', 'W'};
O[10] = {'E', 'R', 'S'};
O[11] = {'E', 'L', 'N'};
O[12] = {'W', 'F', 'W'};
O[13] = {'W', 'B', 'E'};
O[14] = {'W', 'R', 'N'};
O[15] = {'W', 'L', 'S'};

and I get these errors regarding O[13], O[14] and O[15]

..\RMNCODE.C(282): error C231: 'O': redefinition
..\RMNCODE.C(283): error C279: 'O': multiple initialization
..\RMNCODE.C(283): error C231: 'O': redefinition
..\RMNCODE.C(284): error C279: 'O': multiple initialization
..\RMNCODE.C(284): error C231: 'O': redefinition
+8  A: 

Initialize the array like this instead:

struct Orientation O[16] = {
    {'N', 'F', 'N'},
    {'N', 'B', 'S'},
    {'N', 'R', 'E'},
    {'N', 'L', 'W'},
    {'S', 'F', 'S'},
    {'S', 'B', 'N'},
    {'S', 'R', 'W'},
    {'S', 'L', 'E'},
    {'E', 'F', 'E'},
    {'E', 'B', 'W'},
    {'E', 'R', 'S'},
    {'E', 'L', 'N'},
    {'W', 'F', 'W'},
    {'W', 'B', 'E'},
    {'W', 'R', 'N'},
    {'W', 'L', 'S'}
};

Cheers !

Magnus Skog
Can you explain why the O[13] and subsequent lines cause errors?
John Feminella
These errors are the same for all lines. Not just for 13, 14 and 15.
Magnus Skog
I have never seen so many practically identical answers in one question! :)
Aiden Bell
Lol, you had to be fast to grab this reputation. I even managed to compile and test. How about that? ;)
Magnus Skog
@Magnus, lucky lucky ... I was still in vim when the orange-bar-of-death appeared.
Aiden Bell
"orange-bar-of-death" hehehe :)
Magnus Skog
+3  A: 

Either you have to initialize O with a single initializer, or you can assign to its members individually in an initialization function. You can't use assignment expressions outside of a function as you have done.

e.g.

struct Orientation O[16] = { {'N', 'F', 'N'}, {'N', 'B', 'S'} /* , ... */ };

or

void InitializeO(void)
{
    O[0].facing = 'N';
    O[0].sensor = 'F';
    O[0].mazeDir = 'N';

    O[1].facing = 'N';
    O[1].sensor = 'B';
    O[1].mazeDir = 'S';

    /* ... */
}
Charles Bailey
+4  A: 

If you're going to initialize O, you need to do it all at once as part of the declaration:

struct Orientation O[16] = {
  { 'N', 'F', 'N' },
  { 'N', 'B', 'S'),
  ...

};

You can't do this:

O[0] = {'N', 'F', 'N'};

because C doesn't support struct literals in a statement, only as part of an initializer list.

Ferruccio
+1  A: 

You can't assign a struct like you're doing. You need to do it explicitly:

O[0].facing = 'N';
O[0].sensor = 'F';
O[0].mazeDir = 'N';

O[1].facing = 'N';
O[1].sensor = 'B';
O[1].mazeDir = 'S';

//etc...

Or else you can initialize the struct like Magnus suggested.

Nathan Fellman
+2  A: 

Change the assignment by an initialization.

struct Orientation { char facing; char sensor; char mazeDir; };

struct Orientation O[16] = 
{{'N', 'F', 'N'},
{'N', 'B', 'S'}, 
{'N', 'R', 'E'}, 
{'N', 'L', 'W'}, 
{'S', 'F', 'S'}, 
{'S', 'B', 'N'}, 
{'S', 'R', 'W'}, 
{'S', 'L', 'E'}, 
{'E', 'F', 'E'}, 
{'E', 'B', 'W'}, 
{'E', 'R', 'S'}, 
{'E', 'L', 'N'}, 
{'W', 'F', 'W'}, 
{'W', 'B', 'E'}, 
{'W', 'R', 'N'}, 
{'W', 'L', 'S'}};
Max
+1  A: 

Try

struct Orientation O[16] = {
    { 'W','L','S'},
    { 'W', 'R', 'N'},
    ...
}
Aiden Bell
+1  A: 

Although it is not direct answer to your question, you can solve the problem by following syntax:

struct Orientation O[16] = {
  {'N', 'F', 'N' },
  {'N', 'B', 'S'},
  {'N', 'R', 'E'},
  {'N', 'L', 'W'},
  ...
};
+1  A: 

C89 doesn't have literals for structures. It does have initialization lists, which look as if they are literals, but really aren't.

This means that you can only do the "initializing assignment" once, where the variable is defined:

struct Orientation O[16] = {
  {'N', 'F', 'N'},
  ...
};

In C99, this has changed, see the link in the comment.

unwind
http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
sigjuice