On Wed, 19 Jul 2017 09:36:42 -0700 (PDT), kearnh
<
theke...@gmail.com> wrote:
>On Wednesday, 19 July 2017 16:34:27 UTC+2, Michael Keith wrote:
>> Hello everybody,
>> the following C code cannot be parsed by the grammar mentioned above:
>> ---
>> #include <stdio.h>
>>
>> struct myStrT {
>> int i, j;
>> union {
>> char c1;
>> int i1;
>> }; // <- this seems to be the point of failure
>> };
>> ---
>> It errors out with
>> parse error reading "SEMICOLON" in state 183 (line 8, column 7)
>> expecting one of:
>> IDENTIFIER
>> struct_declarator_list
>> COLON
>> LPAREN
>> declarator
>> direct_declarator
>> struct_declarator
>> pointer
>> MULT_STAR
Hi Kearn,
>As far as I can recall anonymous unions (the union is not named inside the struct) are a feature specific to gcc and not actually part of any C standard.
no, my (old) Borland compiler from around 2000 accepts it, too, so I
assume it to be (at least) part of C99..
If you program hardware you often see something like
struct UART_t {
int someReg;
union {
int THR; // the register holding the character to send
int RBR; // the register which holds (buffers) the last
// char received - they are _two_ registers
// residing at the _same_ address
}
...
} *uartPtr;
so you can access all registers via a pointer with the same syntax,
like
uartPtr->someReg = 0xFF;
uartPtr->THR = '?';
recChar = uartPtr->RBR;
>struct myStrT {
> int i, j;
> union {
> char c1;
> int i1;
> } u;
>};
>
>and then address fields like "myStruct.u.c1"
yes, this works, but then the above example would have to be
uartPtr->u->THR = '?';
recChar = uartPtr->u->RBR;
or something similar and I don't think I ever had to do this.
But thanks for answering.
M'