error C2143: syntax error
-
Fighting to solve old plan C errors, I met another issue:
error C2143: syntax error
I have the following code:typedef struct test test\_t; struct test { const char\* name; const char\* name\_option; ... ... };
and
test_t test_123 = {
.name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
.name_option = "Bibi_One",
};How can I get rig of this error ?
-
Fighting to solve old plan C errors, I met another issue:
error C2143: syntax error
I have the following code:typedef struct test test\_t; struct test { const char\* name; const char\* name\_option; ... ... };
and
test_t test_123 = {
.name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
.name_option = "Bibi_One",
};How can I get rig of this error ?
This form of initialization is valid in GNU C, and is valid in other compilers only if they support C99 or later. I don't remember off-hand whether this is supported in C++. You can make this compatible with C89 by adding the missing initializers, e.g.:
test_t test123 = { "Bibi", "Bibi_one", foo, bar };
Where foo and bar are the values for missing members in the struct.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
Fighting to solve old plan C errors, I met another issue:
error C2143: syntax error
I have the following code:typedef struct test test\_t; struct test { const char\* name; const char\* name\_option; ... ... };
and
test_t test_123 = {
.name = "Bibi", // <-- error C2143: syntax error : missing '}' before '.'
.name_option = "Bibi_One",
};How can I get rig of this error ?
Get rid of the member names in the initalization, it's not valid in C++. You can only pass values, and these will be used to initialize the members of the struct in the order of their definition. You don't need to provide values for all members - if you don't, the rest will use default values:
test_t test_123 = { "Bibi", "Bibi_One" };
Special case: if you try to initialize a C array in this manner with less values than it's size, the remainder of the array will be initialized with the last value of your initializer list. This code will initialize your entire array with 1s:
int my_array[9] = {1};
You can read up on this topic in many articles on the web, just use the correct search term: initializer list.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Get rid of the member names in the initalization, it's not valid in C++. You can only pass values, and these will be used to initialize the members of the struct in the order of their definition. You don't need to provide values for all members - if you don't, the rest will use default values:
test_t test_123 = { "Bibi", "Bibi_One" };
Special case: if you try to initialize a C array in this manner with less values than it's size, the remainder of the array will be initialized with the last value of your initializer list. This code will initialize your entire array with 1s:
int my_array[9] = {1};
You can read up on this topic in many articles on the web, just use the correct search term: initializer list.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)