Program for Structure in an Union


    #include <iostream.h>
    #include <conio.h>
    void main ( )
    {
        struct date
        {
            int day ;
            int month ;
            int year ;
        } ;
        union sample
        {
            char *sample_date_1 ;
        struct date sample_date_2 ; /* structure within union */
        } ;
        union sample s ;  /* union variable declaration */
        clrscr ( ) ;
        /* stored as string and retrieved as string */
        s.sample_date_1 = "2 Nov 1999" ;
        cout<<"Date : "<<s.sample_date_1;

        /* stored as structure and retrieved as structure */
        s.sample_date_2.day = 2 ;
        s.sample_date_2.month = 11 ;
        s.sample_date_2.year = 1999 ;
        cout<<"Date  : "<<s.sample_date_2.day;
        cout<<" / "<<s.sample_date_2.month ;
        cout<<"/"<<s.sample_date_2.year ;
        getch () ;
    }

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|