How to Declare a Pointer to a Union in C? Last Updated : 14 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Union is a user-defined data type in C language that can contain elements of the different data types and pointers are used to store memory addresses. In this article, we will learn how to declare a pointer to a union in C++. Pointer to Union in CTo declare a pointer to a union first, define a union and then declare a pointer that points to the union type using the below syntax: Syntax to Declare Pointer to Union in C//Define Unionunion unionName { int var1; float var2;};//declare pointer to unionunion unionName *ptr;We can also assign the pointer the address of some union variable by using the addressof(&) operator. C Program to Declare Pointer to UnionThe following programs show how to declare pointers to unions in C programming and access members using the arrow operator (->). C // C program to declare pointer to union #include <stdio.h> // Defining a union union myUnion { int intValue; float floatValue; char charValue; }; int main() { // Creating a union variable union myUnion u; // Declaring a pointer to the union and assign it the // address of the union variable union myUnion* ptr = &u; // Using the pointer to Set the intValue member of the // union ptr->intValue = 100; // Accessing the intValue member of the union printf("The intValue is: %d\n", ptr->intValue); // set value for floatValue This will overwrite the // intValue ptr->floatValue = 3.14; // Accessing the floatValue member of the union printf("The floatValue is: %f\n", ptr->floatValue); return 0; } OutputThe intValue is: 100 The floatValue is: 3.140000 Comment More infoAdvertise with us Next Article How to Declare a Pointer to a Union in C? H hasani Follow Improve Article Tags : C Programs C Language C-Pointers Pointers C Examples +1 More Practice Tags : Pointers Similar Reads How to Declare a Pointer to a Struct in C? Structure (or structs) in the C programming language provides a way to combine variables of several data types under one name and pointers provide a means of storing memory addresses. In this article, we will learn how to declare such a pointer to a struct in C. Declaration of Pointer to Struct in C 2 min read How to Declare a Struct Member Inside a Union in C? A union contains different types of variables as its members and all these members share a memory location. In this article, we will learn how to declare and access the struct member inside a union in C++. Structure Inside Union in CTo declare a structure inside a union we can use the below syntax: 2 min read How to Use a Union to Save Memory in C? Unions in C offer a unique mechanism for storing different variables in the same memory location. These variables are called members of the union and can be of different types as well. In this article, we will learn how to use a union to save memory in C. Save Memory using Union in CUnion members sh 2 min read How to Create a Typedef for a Function Pointer in C? In C, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. The typedef is a keyword used to create an alias or alternative name for the existing data types. In this article, we will learn how to create a typedef for a function 2 min read How to Create a Linked List in C? Write a C program to create a linked list.A linked list is a sequential data structure that stores the data in non-contiguous memory locations unlike array. In a linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in the 5 min read Like