LinuxQuestions.org
Help answer threads with 0 replies.
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 08-21-2006, 09:58 AM   #1
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Rep: Reputation: 30
unions


why would i as a c programmer use union
instead of struct ?

how is union declared right
and how are fields of an union
accesed correctly?

Last edited by spx2; 08-21-2006 at 10:01 AM.
 
Old 08-21-2006, 10:06 AM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
an union is not a struct, so you normally dont want to use on over the other. Its just a matter of needs. The union lets you store different type of value in one variable (but one at a time). Whereas the struct lets you keep a lot of "variable" into one big variable.
Check this post, as an example, where one solution is to use union: http://www.linuxquestions.org/questi...d.php?t=467530

I hope this is useful
Cheers!
 
Old 08-21-2006, 10:08 AM   #3
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
/* object is a type AND a value */
struct object
{
/* type is an int with 3 sensible values 0, 1 or 2 */
enum { INTEGER_TYPE, FLOAT_TYPE, CHAR_TYPE } type;
/* value can be an int OR a float OR a char */
union { int i; float f; char c;} value;
}

struct object ob1;
ob1.type = CHAR_TYPE;
ob1.value.c = 'x';

struct object ob2;
ob2.type = INTEGER_TYPE;
ob2.value.i = 10;

Last edited by Agrouf; 08-21-2006 at 10:17 AM.
 
Old 08-21-2006, 10:35 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
>why would i as a c programmer use union instead of struct ?
if you want to have a structure that could potentially be many different things but can only be one thing at a time, they are commonly used for a variant in a structure.

>how is union declared right and how are fields of an union accesed correctly?
a union is declared the same way a struct is, only with the struct keyword replaced by union.

using a union will save you space, adn will also let you do fun stuff at a very low level.

here is an example
Code:
#include <stdio.h>

typedef union int32_bytes
{
    int int_value;
    struct { unsigned char b0, b1, b2, b3; } byte_value;
} INT32_VALUE;

int main()
{
    INT32_VALUE v;
    v.int_value = 123456789;
    printf("Int value: %d\n", v.int_value);
    printf("Bytes are: B0: %d, B1: %d, B2: %d, B3: %d\n", 
           v.byte_value.b0, v.byte_value.b1, v.byte_value.b2, v.byte_value.b3);
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
array of unions in c alaios Programming 3 09-19-2005 10:02 AM
array of unions (c code) alaios Programming 6 09-16-2005 12:43 PM
structres and unions problem linux_lover2005 Programming 2 04-15-2005 09:31 AM


All times are GMT -5. The time now is 02:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration