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 CCS Technical Support

problem defining a struct

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



Joined: 25 Jul 2006
Posts: 3

View user's profile Send private message

problem defining a struct
PostPosted: Tue Oct 24, 2006 8:12 am     Reply with quote

I am attempting to define a stuct to make dealing with a data message easier...

I initially defined the struct as:

Code:
struct retrieved_messageB {
   unsigned messageID:    5;
   unsigned int dataA;
   unsigned dataB:         4;
   unsigned dataC:         4;
   unsigned int dataD;
   unsigned int dataE;
   unsigned dataF:         4;
   unsigned dataG:       4;
   unsigned dataH:       6;
   unsigned dataI:         2;
   unsigned dataJ:         1;
   unsigned dataK:         1;
   unsigned spare:         2;   
}


which compiled fine... however, I realised that I had put my stucture in the wrong order. I then carried out the, what I thought was, relatively simple task of re-ording the struct to produce:

Code:

struct retrieved_messageB {
   unsigned spare:         2;
   unsigned dataK:         1;   
   unsigned dataJ:         1;   
   unsigned dataI:         2;   
   unsigned dataH:          6;
   unsigned dataG:         4;
   unsigned dataF:         4;
   unsigned int dataE;
   unsigned int dataD;
   unsigned dataC:         4;
   unsigned dataB:         4;
   unsigned int dataA;
   unsigned messageID:      5;

};


which does NOT compile! giving the error "Error 35... Line 132(17,18): Number of bits is out of range" where line 132 is the line with "unsigned dataH".

Any ideas!?

Thanks,
Shaun
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Oct 24, 2006 8:31 am     Reply with quote

I think it is complaining because they need to line up on byte borders
Code:
 unsigned spare:         2;
   unsigned dataK:         1;   
   unsigned dataJ:         1;   
   unsigned dataI:         2;   
   unsigned dataH:          6;
   unsigned dataG:         4;
 

Can you pad/waist some bits to get them on byte boundaries
Code:
 unsigned spare:         2;
   unsigned dataK:         1;   
   unsigned dataJ:         1;
  unsigned waisted:         4; //8
 
   unsigned dataI:         2;   
   unsigned dataH:          6;//8
   unsigned dataG:         4;//last 4 can trail off i think
 

or you will have to read them in, then make a shift and mask to put them is the data area you want. Perhaps a union will work here?? will a union work with bit places??
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