[FIXED] Bitfields initialization

Started by iZzz32, October 12, 2011, 10:37:56 PM

Previous topic - Next topic

iZzz32

typedef struct Foo
{
  bool              foo:1;
  bool              bar:1;
  bool              baz:1;
  unsigned          qux;
 
} Foo;

Foo f = { true, .bar = 1, 1, 12345 };


The code above does not initialize .bar and .baz to 1, however GCC behaves as expected. Is this a bug?  ;)

(Pelles C 6.50 RC 4, pocc.exe is 6.50.35.0), 7.00 RC 1 too

CommonTater

http://tigcc.ticalc.org/doc/gnuexts.html#SEC82

I think you will find that the .bar=1 syntax is not standard C, but is infact a GCC specific extension to the C language.



Bitbeisser

Quote from: CommonTater on October 13, 2011, 03:12:59 AM
http://tigcc.ticalc.org/doc/gnuexts.html#SEC82

I think you will find that the .bar=1 syntax is not standard C, but is infact a GCC specific extension to the C language.
I am not so sure, as the reference you posted says it's standard in C99 but GCC allows it as an extensio to C89.

Pelles C however claims to be C99 compliant...   ???

Ralf

iZzz32

QuoteI think you will find that the .bar=1 syntax is not standard C
This syntax is standard C99 (ISO/IEC 9899:1999, 6.7.8, paragraph 7) and Pelles C supports it (according to the reference in the help file).

But it does not matter, because Foo f = { 1, 1, 1, 12345 }; does not work either.

iZzz32

Still present in RC3. Is it an error to initialize bit-fields like this? Then why does it work when I change bool to unsigned?
#include <stdbool.h>
#include <assert.h>

typedef struct Foo
{
  bool              a:1;
  bool              b:1;
} Foo;

Foo f = { true, true };

int main(void)
{
  assert(f.a && f.b);
  return 0;
}


Pelle

It looks like a bug, but I have to check. I think the problem is that _Bool is full byte, but you are not allowed to use more than one bit (so you can't pack several bits into a _Bool). At some point I think there was some cunfusion if, for example, "_Bool x:3" was allowed, and if so, what it meant...
/Pelle