ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
let's say i store a value into tag.i (int), after that i retrieve the value by call tag.c (char), is tag.c (char) taking the LSB/MSB value of tag.i (int)? else machine and compiler determine the result?
That'll be machine dependent because endianess will switch which byte its looking at. If you look at it like its flat memory with the integer bytes starting at 0 and the character byte being at 0 then in little endian it'll be LSB and in big endian it'll be MSB that gets mapped to location 0.
For example if the integer was assigned 0x04030201
on a little endian
byte position
0 1 2 3
01 02 03 04
on a big endian
byte position
0 1 2 3
04 03 02 01
Last edited by estabroo; 07-03-2008 at 09:52 PM.
Reason: wanted to add an example
Your solution will either be in conditional compiling (ifdefs) based on the machine (i386, 68000, ...) or you can have a look at the functions that convert integers from host byte order to network byte order (htonl/htons and the reverse ntohl/ntohs).
On another note:
I thought that the elements of an union we're supposed to be of the same size (so char[2] or char[4] in your case); I might however be wrong
On another note:
I thought that the elements of an union we're supposed to be of the same size (so char[2] or char[4] in your case); I might however be wrong
I do not understand what you mean by the highlighted text, but the size of elements in a union does not matter the union will use a type which can hold the largest element IIRC.
Since structure alignment can affect an applications performance, sometimes it is best to ensure structures are properly aligned the hardwares natural size boundaries. Padding a union is sometimes a good idea in such cases. But the language ensures that the types in the union are accessible regardless of union member size differences.
That'll be machine dependent because endianess will switch which byte its looking at. If you look at it like its flat memory with the integer bytes starting at 0 and the character byte being at 0 then in little endian it'll be LSB and in big endian it'll be MSB that gets mapped to location 0.
For example if the integer was assigned 0x04030201
on a little endian
byte position
0 1 2 3
01 02 03 04
on a big endian
byte position
0 1 2 3
04 03 02 01
Thanks estabroo, i got a clearer picture about union d.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.