Objective-C: Unions & Enumerators

Union are same as that of structure with a major difference of storage mechanism. In structures, separate memory block is allocated for all the data members, whereas, in case of union, only one memory block is allocated that is of the largest data type defined.

  

    Syntax:

union name

{

    data-type var1;

    data-type var2;

       ………;

    data-type varn;

} u1,u2,..;


Like nested structures, nesting of unions is performed and we can also integrate structures and union together in a single program.

 

 

     Syntax:

struct student

{

    char* name;

 

   union marks

   {

    float mark;

    int roll;

      }mrk; 

   

    }s;

 

Program

  #import <Foundation/Foundation.h>

 int main (int argc, const char *argv[]){

union student

{

    char* name;

    float mark;

    int roll;

    }s;   

   

         NSLog(@“Enter Name of the student : ”);

         Scanf(“%s”,&s.name);

         NSLog(@“Enter Roll : ”);

         Scanf(“%i”,&s.roll);

         NSLog(@“Enter Marks obtained : ”);

         Scanf(“%f”,&s.mark);

    

   NSLog(@“\tRecord of student”);

 

         NSLog(@“NAME   :%s”,s.name);

         NSLog(@“Roll   :%i”,s.roll);

         NSLog(@“Marks  :%f”,s.mark);

     

 return 0;

}

Output:

Enter Name of the student: Alice

Enter Subject : Computers

Enter Marks obtained: 86

      Record of student

NAME   : Alice

Subject: Computers

Marks  : 86

 

Memory allocation in Union

 

 

As float type has maximum storage capacity as compared to int and char,   memory of 4 bytes is allocated to the variable s defined for union. As it stores one value at a time, all the three values are stored in the same memory block one by one.

Enumerators also referred as to Enumerated data types.

  • A user defined data type where a list of fixed set of named values is created using a variable of type enum that represents only integer constants.
  • The default value of the identifiers begins with 0, like array. And if only first identifier is initialized by any value, the next is incremented by 1 and so on.

 

     Syntax:

typedef enum {

        value1;

        value2;

           ………;

        valuen;

} nameOfEnumeration;

 

Program

 

#import <Foundation/Foundation.h>

 int main (int argc, const char *argv[]){

 typedef enum values {

    firstvalue = 45;

    secondvalue;

    thirdvalue;     

     }d;       

 

typedef enum newvalues {

    firstval;

    secondval;

     }d;       

   

   NSLog(@“\tValues of enumerator – values are”);

         NSLog(@“first value = %i”,firstvalue);

         NSLog(@“Second value = %i”,secondvalue);

         NSLog(@“Third value  = %i”,thirdvalue);

   NSLog(@“\tValues of enumerator – newvalues are”);

         NSLog(@“first value = %i”,firstval);

         NSLog(@“Second value = %i”,secondval);

 

 return 0;

}

Output:

      Values of enumerator – values are

first value = 45

Second value = 46

Third value  = 47

      Values of enumerator – newvalues are

first value = 0

Second value = 1

 

 Explanation

 

  • In first enumerator – value, only the first element is initialized as 45 and rest are not given any value by the developer, but compiler automatically generates value for other members by incrementing each new element by 1. Thus, value of the second and third element becomes 46 and 47.

In second enumerator – newvalues, no element is initialized by the developer, thus, compiler automatically generates value = 1 for the first element and then incrementing each element by 1.