> -----Original Message----- > From: Mike Harrison [SMTP:mike@whitewing.co.uk] > Sent: Tuesday, October 15, 2002 3:57 PM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [pic]: C pointer/union syntax > > >Personaly I rarely bother with using structures in this manner. They can > be > >usefull, but one you start messing around with pointers it gets messy and > >you might as well access the bytes within the long directly using > pointers, > >e.g. > > > >void readadc(long *result) > >{ > >*(((unsigned char *)result)+0) = getbyte(); > >*(((unsigned char *)result)+1) = getbyte(); > >*(((unsigned char *)result)+2) = getbyte(); > >} > Thanks for that hint - makes things much simpler! > Incidentally is there a similar trick for accessing bytes within > global longs without having to use a union, > e.g. > long reading; > > = byte1; > Yes, I use some macros to do this: #define byte0(x) (unsigned char)(*(((unsigned char *)&x)+0)) #define byte1(x) (unsigned char)(*(((unsigned char *)&x)+1)) #define byte2(x) (unsigned char)(*(((unsigned char *)&x)+2)) #define byte3(x) (unsigned char)(*(((unsigned char *)&x)+3)) Cheers Mike -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics