
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain the Union to pointer in C language
A union is called as a memory location, which is shared by several variables of different data types.
Syntax
The syntax is as follows −
union uniontag{ datatype member 1; datatype member 2; ---- ---- datatype member n; };
For example,
union sample{ int a; float b; char c; };
Declaration of union variable
Given below are the respective declarations of union variable −
Union sample
{ int a; float b; char c; }s;
Union
{ int a; float b; char c; }s;
Union sample
{ int a; float b; char c; }; union sample s;
When union is declared, the compiler automatically creates a variable which hold the largest variable type in the union.
At any time, only one variable can be referred.
Initialization and accessing
- Accessing union member is same as the structure.
- Generally, the dot operator is used for accessing members.
- The arrow operator ( ->) is used for accessing the members
- There is no restriction while using data type in a union.
Example
Following is the C program for union to pointer −
#include<stdio.h> union abc{ int a; char b; }; int main(){ union abc var; var.a=90; union abc *p=&var; printf("%d%c",p->a,p->b); }
Output
When the above program is executed, it produces the following result −
90Z
- Related Articles
- Explain the concept of pointer to pointer and void pointer in C language?
- Explain the concept of pointer accessing in C language
- Explain the concept of union of structures in C language
- Explain the dynamic memory allocation of pointer to structure in C language
- How to define pointer to pointer in C language?
- Differentiate the NULL pointer with Void pointer in C language
- Explain the context free language closure under union operation?
- How to access the pointer to structure in C language?
- Explain the concept of Array of Pointer and Pointer to Pointer in C programming
- What is void pointer in C language?
- What is union of structure in C language?
- Explain the pointers to unions in C language
- How to create a pointer for strings using C language?
- Double Pointer (Pointer to Pointer) in C
- What do you mean by pointer to a constant in C language?

Advertisements