LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2008, 09:05 PM   #1
retnuoc
LQ Newbie
 
Registered: Apr 2006
Posts: 2

Rep: Reputation: 0
C - Union


Hello, i meet a C problem recently, a union:

union u_tag {
char c;
int i;
}tag;

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?

Last edited by retnuoc; 07-03-2008 at 09:09 PM.
 
Old 07-03-2008, 09:50 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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
 
Old 07-04-2008, 12:51 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
As stated by estabroo, it's machine dependent.

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).

WimS
 
Old 07-04-2008, 02:12 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 07-04-2008, 03:15 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Wim Sturkenboom View Post
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.
 
Old 07-04-2008, 03:26 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Yes, a union will occupy the largest space to accommodate the largest member.
 
Old 07-05-2008, 03:39 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Sorry, I've always assumed that if you have 16 (8-bit) chars in a union, you also needed to have 4 32-bit integers in there.

From your comments, it's obvious that I was wrong.
 
Old 07-05-2008, 12:25 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 07-07-2008, 01:51 AM   #9
retnuoc
LQ Newbie
 
Registered: Apr 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by estabroo View Post
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.

Last edited by retnuoc; 07-07-2008 at 02:43 AM.
 
  


Reply

Tags
char, int, member, union



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
typedef a union as a struct? KasperLotus Programming 3 03-09-2007 01:10 PM
LXer: Promising union LXer Syndicated Linux News 0 05-09-2006 07:03 AM
union field length mismatch linux_lover2005 Programming 4 05-22-2005 02:28 PM
union wait problem zaichik Programming 3 12-22-2004 09:59 AM
mpaa & europe union? annehoog General 1 01-09-2003 11:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration