union
{
__commandstruct commandstruct;
long commandword;
};
i should then be able to get the elements like so..
comamandword= 0xffff;
commandstruct.CO=false
etc..
it no work like that?? as far as i can tell its of the txtbook..
!
!
\\\\\\/
[.] [.]
\
____
/ \
___________________________
This message was ported from CCS's old forum
Original Post ID: 6014
kernel panic Guest
Re: unions of structs...
Posted: Thu Aug 01, 2002 3:02 pm
it seems nesting the struct inside the union works.. perhaps the compiler doesn't like previously defined typedefs inside of new typedefs..
:=im trying to create a structure for peeling apart a 16b command word.. why doesn't this work??
:=
:=struct __commandstruct
:={
:= byte C0:1;
:= byte uu:4;
:= byte Ps:3;
:= byte timerval;
:=};
:=
:=union
:={
:= __commandstruct commandstruct;
:= long commandword;
:=};
:=
:=
:=i should then be able to get the elements like so..
:=
:=comamandword= 0xffff;
:=
:=commandstruct.CO=false
:=etc..
:=
:=
:=it no work like that?? as far as i can tell its of the txtbook..
:=
:= !
:= !
:=\\\\\\/
:=[.] [.]
:= \
:= ____
:= / \
___________________________
This message was ported from CCS's old forum
Original Post ID: 6015
Woody Guest
Re: unions of structs...
Posted: Wed Aug 07, 2002 1:53 am
Your example code does not use typedef to define a new user type and therefore your union should look like this:-
union
{
struct __commandstruct commandstruct;
long commandword;
};
The following example shows two ways of defining the structure and overlay. Either method will work but using typedef's is usually easier.
typedef struct tagCommandstruct {
unsigned int C0:1;
unsigned int uu:4;
unsigned int Ps:3;
unsigned int timerval;
} Commandstruct;
/* Example 1 using newly defined type */
typedef union tagDatamap {
Commandstruct commandstruct;
long commandword;
} Datamap;
#else
#define DATA_OVERLAY_TYPE union tagDatamap
struct tagCommandstruct {
unsigned int C0:1;
unsigned int uu:4;
unsigned int Ps:3;
unsigned int timerval;
};
/* Example 2 using struct tagCommandstruct */
union tagDatamap {
struct tagCommandstruct commandstruct;
long commandword;
};
#endif
int main (void)
{
DATA_OVERLAY_TYPE v;
v.commandstruct.C0 = 1;
}
/************************************/
PS.
It's not a good idea to use identifiers starting underscore. I believe the ANSI standard for 'C' reserves identifiers starting with double underscore __ and underscore following by a capital letter. That's not to say user identifiers defined like this this will not work, but it could easily cause an obscure problem one day.
___________________________
This message was ported from CCS's old forum
Original Post ID: 6134
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum