M
Michael
In the past I have developed many micro-controller products using
assembler. I have recently started using C, and so far I love it! I am
very motivated to learn and become accustomed to the language.
I have a couple of simple questions; just to make sure I'm not going
about things the wrong way.
When writing functions that handle four bytes, I use the 'unsigned
long' variable type. At first to access the individual bytes of an
unsigned long, I would create a union as such:
union {
unsigned long longword;
char byte[4];
} varname;
Then use varname.longword or varname.byte[0-3] as required.
But while experimenting, I found a better way: (&(char)longword)[0-3]
Is it ok for code to be dependant on big or little endian? Or does
this indicate bad programming?
There are so many ways to do things in C. I'd appreciate any a
guidance you might have to offer.
Regards, Michael.
assembler. I have recently started using C, and so far I love it! I am
very motivated to learn and become accustomed to the language.
I have a couple of simple questions; just to make sure I'm not going
about things the wrong way.
When writing functions that handle four bytes, I use the 'unsigned
long' variable type. At first to access the individual bytes of an
unsigned long, I would create a union as such:
union {
unsigned long longword;
char byte[4];
} varname;
Then use varname.longword or varname.byte[0-3] as required.
But while experimenting, I found a better way: (&(char)longword)[0-3]
Is it ok for code to be dependant on big or little endian? Or does
this indicate bad programming?
There are so many ways to do things in C. I'd appreciate any a
guidance you might have to offer.
Regards, Michael.