Atmel CAVR-4 Manual De Usuario

Descargar
Página de 323
CAVR-4
272
Descriptions of language extensions
AVR® IAR C/C++ Compiler
Reference Guide
For example, the structure 
str
 in the following example contains an anonymous union. 
The members of the union are accessed using the names 
b
 and 
c
, for example 
obj.b
.
Without anonymous structure types, the union would have to be named—for example 
u
—and the member elements accessed using the syntax 
obj.u.b
.
struct str 
{
  int a;
  union 
  {
    int b;
    int c;
  };
};
struct str obj;
Bitfields and non-standard types
In ISO/ANSI C, a bitfield must be of the type 
int
 or 
unsigned int
. Using IAR 
language extensions, any integer types and enums may be used. 
For example, in the following structure an 
unsigned
 
char
 is used for holding three 
bits. The advantage is that the struct will be smaller.
struct str 
{
  unsigned
 
char bitOne   : 1;
  unsigned
 
char bitTwo   : 1;
  unsigned
 
char bitThree : 1;
};
This matches G.5.8 in the appendix of the ISO standard, ISO Portability Issues.
Incomplete arrays at end of structs
The last element of a 
struct
 may be an incomplete array. This is useful because one 
chunk of memory can be allocated for the 
struct
 itself and for the array, regardless of 
the size of the array.
Note: The array may not be the only member of the 
struct
. If that was the case, then 
the size of the 
struct
 would be zero, which is not allowed in ISO/ANSI C.