Array of pointers to structure

/*Array of pointers to structure */
    #include <iostream.h>
    #include <conio.h>
    struct sample
    {
        int a ;
        char b ;
    } ;
    void main ()
    {
        /* Array of pointers to structure declaration */
        struct sample *array[5] ;
        /* structure declaration and initialisation */
        struct sample s1 = { 1, 'A' } ;
        struct sample s2 = { 2, 'B' } ;
        struct sample s3 = { 3, 'C' } ;
        struct sample s4 = { 4, 'D' } ;
        struct sample s5 = { 5, 'E' } ;
        int i ;
        clrscr ();
        array[0] = &s1;        /* pointer assignment */
        array[1] = &s2;
        array[2] = &s3;
        array[3] = &s4;
        array[4] = &s5;
              for ( i = 0; i < 5; i++ ) /* accessing through pointers */
        {
            cout<<"Contents of structure ... "<<i+1 ;
            cout<<"a :  "<<(*array[i]).a<<endl ;
            cout<<"b :  "<<(*array[i]).b<<endl ;
        }
        getch ();
    }

0 comments:

Post a Comment

 
 
 
 


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