1

Trying to initialize structure variable fields with values in short way:

typedef struct
{
    int id = 0;
    char* name = "none";
}employee;

employee e = 
{
    .id = 0 ;
    .name = "none" ;
};

Got error in e initialization:

Error    expected ‘}’ before ‘;’ token                                                  
Note     to match this ‘{’                                                              
Error    could not convert ‘{0}’ from ‘<brace-enclosed initializer list>’ to ‘employee’ 

Why I'm getting error and how to solve this problem?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
vico
  • 17,051
  • 45
  • 159
  • 315

1 Answers1

0

In C you may not initialize data members in a structure definition. So you have to write

typedef struct
{
    int id;
    char* name;
}employee;

Also the separator in the list initialization is comma.

employee e = 
{
    .id = 0,
    .name = "none",
};

or

employee e = 
{
    .id = 0,
    .name = "none"
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335