·  C++ – Syntax – Pointers – To Union

 

- Info:

  - Pointers to unions behave the same as pointers to struct.

 

- Example:

  - This example shows basic operations with pointers to unions.

  - Create C++ Console Application Test.cpp.

 

    Test.cpp

    #include <stdio.h>

   

    int main() {

   

      // DECLARE UNION PERSON.

      union person{

        int  id;

        char password[10];

      } john;

   

      // CREATE POINTER TO UNION PERSON.

      union person *pts = &john;

   

      // ACCESS VALUE OF UNION VARIABLES.

      (*pts).id = 20;

        pts->id = 20;

   

      // GET VALUE OF UNION VARIABLES.

      printf("%i", john.id);

   

      return 1;

   

    }