Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-21-2006, 08:58 AM   #1
Registered: Dec 2005
Distribution: debian
Posts: 160

Rep: Reputation: 30

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 09:01 AM.
Old 08-21-2006, 09:06 AM   #2
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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:

I hope this is useful
Old 08-21-2006, 09:08 AM   #3
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 */
/* 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 09:17 AM.
Old 08-21-2006, 09:35 AM   #4
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
#include <stdio.h>

typedef union int32_bytes
    int int_value;
    struct { unsigned char b0, b1, b2, b3; } byte_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;


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 09:02 AM
array of unions (c code) alaios Programming 6 09-16-2005 11:43 AM
structres and unions problem linux_lover2005 Programming 2 04-15-2005 08:31 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

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