-1
typedef struct {
  char fielda[ 2 ][ FIELD_A_MAX + 1 ];

  bool fieldb = false;
  bool fieldc = false;
  sem_t fieldd;
} Set;

I get the error:

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token bool fieldb = false;

What's my mistake here?

der_Fidelis
  • 1,475
  • 1
  • 11
  • 25

2 Answers2

1

You cannot initialize fields inside a type definition. All initialization has to happen at the time of declaring a variable of the defined type:

typedef struct {
  char fielda[ 2 ][ FIELD_A_MAX + 1 ];

  bool fieldb;
  bool fieldc;
  sem_t fieldd;
} Set;
...
Set s = {.fieldb = false, .fieldc = false};

Unfortunately, the initializing sequence needs to be repeated each time. To avoid this, you could make a function to initialize Set:

void init_Set(Set* s) {
    s->fieldb = false;
    s->fieldc = false;
    ...
}

Now the initialization code is in a single place. You need to invoke this code for each Set structure that you allocate.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

In C is not possible to initialize members in structures.

bogdan tudose
  • 1,064
  • 1
  • 9
  • 21