What does this C error mean regarding structures?

Can 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
+3
source share
9 answers

Initialize the array as follows:

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'}
};

Greetings!

+8
source

If you are going to initialize O, you need to do all this at once as part of the declaration:

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

};

You cannot do this:

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

because C does not support string literals in an instruction, only as part of a list of initializers.

+5
source

O , . , .

.

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

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';

    /* ... */
}
+3

.

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'}};
+2

, . :

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...

, .

+1

Try

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

, , :

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

C89 . , , , .

, " " , :

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

C99 , . .

+1

- , , , , , -

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

The advantage is that it can be executed everywhere inside a function, and not just during initialization.

Although, like everyone else, you can simply rewrite it as:

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

Or even

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

If you just want to assign values ​​during initialization.

0
source

Source: https://habr.com/ru/post/1709018/


All Articles