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 03-18-2010, 07:34 AM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
C Unions


I was wondering how to use unions in C? What are they practically useful for and what can you do with them?
 
Old 03-18-2010, 08:15 AM   #2
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Unions are good for being able to access and store several different types in a single address.

Code:
struct var {
    type_t type;
    union {
         char character;
         char* string;
         int number;
    } value
}
This structure lets you store three different types (char, char*, int). The maximum size size of var is size_of(type_t) + size_of(int).

Code:
void print_var(struct var v)
{
    switch (v.type) {
        case NUMBER:
            printf("%d", v.number);
            break;
        case STRING:
            printf("%s", v.string);
            break;
        case CHARACTER:
            printf("%c", v.character);
            break;
    }
}

Last edited by mjones490; 03-18-2010 at 08:23 AM.
 
1 members found this post helpful.
Old 03-18-2010, 08:43 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Original Poster
Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
That's pretty neat, but just one thing: howcome it's "v.number", not "v.value.number"?
 
Old 03-18-2010, 08:49 AM   #4
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Quote:
Originally Posted by MTK358 View Post
That's pretty neat, but just one thing: howcome it's "v.number", not "v.value.number"?
Good catch. If I'd run it through a compiler I'd have noticed it.

Yes, it's v.value.number, v.value.string, and v.value.character.
 
Old 03-18-2010, 08:57 AM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,083

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by mjones490 View Post
The maximum size size of var is size_of(type_t) + size_of(int).
It can easily be larger than that.

First, char* could easily be larger than int.

Second, type_t might be an odd size and the architecture might have an alignment requirement for either char* or int.

So, the idea is valid (the union has enough space for just the largest of its members, not the sum of the sizes of its members). But the way you expressed it was not correct.

Quote:
Originally Posted by MTK358 View Post
What are they practically useful for
That is a harder question.

There are situations where you want some polymorphic structure and you want to minimize the wasted space of the elements that don't apply to the current "shape" of the structure. In C++, that is usually better accomplished by multiple structures inheriting from a common base class. But in C, you don't have inheritance, so a workable alternative is a struct consisting of an union, plus an enum (or similar) to tell which member of the union is valid.

There are also cases in both C and C++, where you want the conceptual equivalent of a C++ reinterpret_cast. Reinterpret a pointer or reference to one type as a pointer or reference to another type in order to cause the referenced bit pattern to be read and reinterpreted (not translated) as a different type from the one that was written there. As I understand the aliasing rules of C++, a reinterpret_cast used in that way will under many conditions be broken by the optimizer. So even in C++, you may need a union to do what you'd like a reinterpret_cast to do. In C, a simple cast (all you have) of a pointer does have the semantics of a C++ reinterpret_cast, but a union is probably the safer way to accomplish a reinterpret of a bit pattern as a different type.

Any reinterpret of a bit pattern as a different type is inherently non portable (unlike the other use of union I described first). But sometimes it is the only effective tool for the job.

Last edited by johnsfine; 03-18-2010 at 09:12 AM.
 
  


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
C++ Unions to understand - question vargadanis Programming 14 04-19-2007 11:49 PM
unions spx2 Programming 3 08-21-2006 09:35 AM
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


All times are GMT -5. The time now is 05:13 AM.

Main Menu
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