Pointer To Union










  • Before taking a deep dive into this topic , We suggest you to please go through to this topics : What are pointers? , Dynamic memory allocation , and What are Unions ?

    • As Pointer are the variable which stores the address of other variable.
    • So , Now we will learn how store the address of Union variable.
    • As you know pointer can point to a variable, only if their data type are same.
    • For example:int type of pointer can only point to int type of variable or array,etc
    • Because of that ,for pointing a Union you will need to make a pointer of Union datatype. So now we will learn how to do all that things
    • So , let start doing it.
    • for learning that we will define a Union student as name , rollno ,div as it's member.

    Definition:

    union student  //union name
    {
        int  rollno; //member1 ,integer variable which will store,
                     //student roll no.
                     
        char  name[20]; //member2 a character array of 20 character
                        //which will store name of student.
                        
        char  div;  //member3 a character variable,
                  //which will store division of student.
    };
    

    As we have define a union, now we will declare a pointer of union student data type.

    Syntax:declaring pointer of union student data type
    union union_name  *pointer_variable_name;
    

    Declaring:pointer of union student datatype.
    union  student  *s;
    

    Let's see memory view of union pointer,by looking it you will get an great understanding of pointer of union variable




    Now we will access the member of pointer of union variable

    Syntax: Accessing the member
    pointer_variable_name  -> member_name;
    
    • We use arrow operator for accessing the member of union, when union is point by a pointer.
    • -> Arrow operator.

    Accessing the members:
    //accessing rollno
    s -> rollno;
    
    
    //Accessing Name
    
    s -> name;
    
    
    //Accessing div
    
    s -> div
    

    Now we will take members as input from the user.
    void main()
    {
    int i;
    //declaration of pointer variable of
    //union student data type.
    union student *s,s1;
    s = &s1;
    //Assigning address of union 
    //variable s1 to pointer variable 5
    
    
    //Taking student roll no as input
    printf("\nEnter the roll no of student:");
    scanf("%d",&s->rollno);
    
    //Taking student name as input
    printf("\nEnter the name of student:");
    scanf("%s",s->name);
    
    //Taking student Division as input
    printf("\nEnter the student div:");
    (s->div) = getche();
    
    
    //Printing the values which is taking from the user. 
    printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div);
        
        }
    

    Output:
    Enter the roll no of student: 1
    Enter the name of student: Roy
    Enter the student div:A

    student roll no and name is 1 and Roy and studies in A Division.


    Dynamic memory allocation

    • Sometimes, the number of union variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming

    In this program we will use malloc() function

    Syntax:

    pointer_name  =  (union _name  *)   malloc(sizeof(union_name)); 
    

    Allocating memory dynamically: to pointer of student union datatype
    void main()
    {
    //declaration of pointer variable of
    //union student data type.
    union student *s;
    s = (union student*)malloc(sizeof(union student)*1);
    //allocating memory dynamically using malloc()
    
    

    Let's take input from the user
    printf("\nEnter the roll no of student:");
    scanf("%d",&s->rollno);
    
    //Taking student name as input
    printf("\nEnter the name of student:");
    scanf("%s",s->name);
    
    //Taking student Division as input
    printf("\nEnter the student div:");
    (s->div)=getchar();
    

    Let's print the data on the screen
    //printing rollno
    printf("\n%d",s->rollno);
    //printing name
    printf("\n%s\N",s->name);
    //printing div
    printf("\n%c",s->div);
    
    }
    

    • This how we can point to a union variable dynamically.
    • So ,do more practice on pointers and union for understanding the concept more better.






    Related Concept:



    People Also Searched: