mbeddr / mbeddr.core

The mbeddr core. An extensible C
Eclipse Public License 2.0
224 stars 77 forks source link

Explicit initialization of structures with boolean values using a default init "{0}" leads to type-system error #1682

Open danielratiu opened 7 years ago

danielratiu commented 7 years ago

struct s1 { boolean b1; boolean b2: }

s1 v = {0} <-- type error (see screenshot)

image

andreasgrosche commented 7 years ago

Regarding pure C, the initializer list initializes the named elements of the struct, starting with the first element. The types of the elements in the initializer list have to match the types of the struct members in the same order. The missing elements are initialized "implicitly the same as objects that have static storage duration" ("false" in this case).

E.g. the following code works for C (the first element is explicitely initialized with false and all other elements are implicitly initialized with the default value for static objects "false"):

struct s1 { 
  boolean b1; 
  boolean b2; 
};

s1 v = {false};  

Unfortunately, mbeddr complains, "wrong number of arguments".

For initialization in general, see also http://stackoverflow.com/questions/11152160/initializing-a-struct-to-0.

danielratiu commented 7 years ago

@andreasgrosche AFAIK each structure can be initialized (default) with "structVar = {0};"

We should allow this in mbeddr as well.

coolya commented 7 years ago

@danielratiu an empty initialiser list should work and give you the expected result.

coolya commented 7 years ago

@andreasgrosche yes C permits that but mbeddr currently not. But it would make good first contribution if you like 😉 We are always happy to see pull requests.

Though we might want to place a warning in theses cases. Not 100% sure putting @stephaneberle9 in the loop for his opinion.