CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

unions of structs...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kernel panic
Guest







unions of structs...
PostPosted: Thu Aug 01, 2002 2:48 pm     Reply with quote

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: 6014
kernel panic
Guest







Re: unions of structs...
PostPosted: Thu Aug 01, 2002 3:02 pm     Reply with quote

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...
PostPosted: Wed Aug 07, 2002 1:53 am     Reply with quote

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.

/**********************************/
#include <16f876.h>

#pragma CASE

#define FIRST_METHOD
#ifdef FIRST_METHOD

#define DATA_OVERLAY_TYPE Datamap

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group