Home > DeveloperSection > Articles > Objective-C : Unions

Objective-C : Unions

iPhone iPhone  Ios  Objective C 
0 Comment(s)
 1559  View(s)
Rate this:

Objective-C : Unions   

Previously we learn how does structure works in ObjC : Objective-C : Structures


Unions are a data construct that allow you to store more than one type of data in the same storage area. They are typically used in more advanced constructs.Unions are syntactically similar to structs, but inside there are not three pieces of memory, only one. You can used the mixed type to hold either an int, or float, or char If you needed a variable, x, that could hold either an int, float, or char type, you could define a union as such:

union mixed {

      int i;

     float f;

    char c;


Unions are syntactically similar to structs, but inside there are not three pieces of memory, only one. You can used the mixed type to hold either an int, or float, or char, but not two at the same time. Setting a new value overrides the previous, and you must ensure that you pull out the same type as you put in; otherwise you’ll get strange results. Accessing the field is the same as with a struct:

// x is such a union type

x.c = 'E';

x.f = 39.28;

x.i = 7/2;

Because the float, char, and int members of x coexist in the same place in memory, only one value can be stored in x at a time. Furthermore, you must ensure that the value retrieved from a union is consistent with the way it was last stored in the union.

When defining a union the name of the union is not required and variables can be declared at the same time that the union is defined.You can also declare pointers to unions, and their syntax and rules for performing operations are the same as for structures. Finally, you can initialize the union with a single value in braces, and it will be assigned to the first field:

union mixed x = {'$'};

// You can also specify a field

union mixed x = {.f = 3.14};

You can initialize an automatic union variable to another union variable of the same type. 


#import <Foundation/Foundation.h>
int main()
    union mixed {int i; float f; char c;}m;
    m.c = 'E';
    m.f = 39.28;
    m.i = 7/2;
    NSLog(@"%d", m);
    return 0;

when we execute the above code the result will be:

2015-07-13 09:21:27.706 demo[28944] 3


A union enables you to define arrays that you can use to store elements of different data types. For example, the following statement sets up an array called table, consisting of kTableEntries elements:

    char *name;
    int type;
        int i;
        float f;
        char c;
    } data;
}table [kTableEntries];

Each element of the array contains a structure consisting of a character pointer called name, an integer member called type, and a union member called data. Each data member of the array can contain an int, a float, or a char. You might use the integer member type to keep track of the type of value stored in the member data. 

Points to be remember:

1.       Union data structures allow ambiguity

2.       Useful for allowing one storage area to hold different variable types

3.       Can only hold one of the variables at a time.

4.       Must be careful to ensure retrieval type matches last type stored.

5.       Might come in handy for storing lists of “data” that might be of different types.

6.       The last example gives ones potentially practical use;

7.       A series of recordings store 'mData'.

8.       The struct has a char to indicate the type of data recorded in mData.


Next, we will learn about : Objective-C : Preprocessor

Don't want to miss updates? Please click the below button!

Follow MindStick