typedef union and struct

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
jeflores
Posts: 12
Joined: Fri Apr 06, 2012 7:37 am

typedef union and struct

Post by jeflores »

hello
how I can define the following structure in C code to Flowcode.
The following example gives me compile error, missing bracket.
Flowcode1.c(1364): error: missing right brace

typedef union ELINMINT_MESSAGE_SIZE
{
unsigned char SIZE;

struct
{
unsigned SIZE0:1;
unsigned SIZE1:1;
unsigned SIZE2:1;
unsigned SIZE3:1;
unsigned SIZE4:1;
unsigned SIZE5:1;
unsigned SIZE6:1;
unsigned SIZE7:1;
} SIZEbits;
} ELINMINT_MESSAGE_SIZE;

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: typedef union and struct

Post by JonnyW »

Hello. Try creating the struct as a typedef - I don't know if BoostC supports nested unnamed structures:

Code: Select all

typedef struct ELINMINT_MESSAGE_BITSIZE
{
unsigned SIZE0:1;
unsigned SIZE1:1;
unsigned SIZE2:1;
unsigned SIZE3:1;
unsigned SIZE4:1;
unsigned SIZE5:1;
unsigned SIZE6:1;
unsigned SIZE7:1;
} ELINMINT_MESSAGE_BITSIZE;

typedef union ELINMINT_MESSAGE_SIZE
{
unsigned char SIZE;
ELINMINT_MESSAGE_BITSIZE SIZEBits;
} ELINMINT_MESSAGE_SIZE;
Also, unless your code is already written using this I would not recommend using bit-fields for every bit as you can't access them programatically and have to resort to nested 'if' statements or a 'switch' statement. This is an alternative:

Code: Select all

#define ELINMINT_MESSAGE_BIT(byte, bit) (((byte) >> (bit)) & 1)
typedef unsigned char ELINMINT_MESSAGE_SIZE;
// Example: SIZE1 = ELINMINT_MESSAGE_BIT(SIZE, 1);
The C compiler will have to perform this shift operation anyway for your bit-fields so I'd be surprised if its less efficient.

Cheers,

Jonny

Post Reply