Page 126 - 6437
P. 126

be used to store multiple types of data. You can use any built-in or user-defined data types
                   inside a union based on your requirement.
                          The memory occupied by a union will be large enough to hold the largest member of the
                   union. For example, in the above example, Data type will  occupy
                          20 bytes of memory space because this is the maximum space which can be occupied by a
                   character string. The following example displays the total memory size occupied by the above
                   union:


                 #include <stdio.h>
                 #include <string.h>


                 union Data

                 {
                    int i;

                    float f;
                    char  str[20];

                 };


                 int main( )
                 {

                    union Data data;


                    printf( "Memory size occupied by data : %d\n", sizeof(data));


                    return 0;
                 }

                          When the above code is compiled and executed, it produces the following result:

                 Memory size occupied by data : 20



                          Accessing Union Members
                          To access any member of a union, we use the member access operator (.). The member
                   access operator is coded as a period between the union variable name and the union member that
                   we wish to access. You would use the keyword union  to define  variables of union type. The
                   following example shows how to use unions in a program:








                 #include <stdio.h>

                                                                                                     120
   121   122   123   124   125   126   127   128   129   130   131