---
title: "Objective-C : Unions"  
description: "Unions are syntactically similar to structs, but inside there are not three pieces of memory, only one."  
author: "Tarun Kumar"  
published: 2015-07-13  
updated: 2020-02-03  
canonical: https://www.mindstick.com/articles/1820/objective-c-unions  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 4 minutes  

---

# Objective-C : Unions

*Previously we learn how does [structure](https://www.mindstick.com/forum/1188/how-to-create-structure-in-c) 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 typex.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](https://www.mindstick.com/articles/715/php-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](https://www.mindstick.com/news/2675/bmw-agrees-to-integrate-blockchain-with-operations-partners-with-bnb-chain-coinweb) 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 fieldunion mixed x = {.f = 3.14};
```

\

You can initialize an [automatic](https://answers.mindstick.com/qa/45147/what-is-the-rule-relating-to-automatic-suspension-of-a-member) union variable to another union variable of the

\

same type.

\

##### Example:

\

```
#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](https://answers.mindstick.com/qa/62167/which-state-government-sets-up-a-skill-university-with-a-cost-of-850-crores) an array called table, consisting of kTableEntries elements:

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

\

Each [element of the array](https://www.mindstick.com/forum/158053/how-to-remove-the-matching-element-of-the-array-in-javascript) contains a structure consisting of a [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-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](https://www.mindstick.com/blog/301312/data-structures-and-their-applications) 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](https://yourviews.mindstick.com/view/81129/work-from-home-proven-practical-during-corona-pandemic) use;

\
7-A series of recordings store 'mData'.

\
9-The struct has a char to indicate the type of data recorded in mData.

*Next, we will learn about:* Objective-C : Preprocessor

---

Original Source: https://www.mindstick.com/articles/1820/objective-c-unions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
