Skip to main content

Accessing Union Members in C

Accessing Union Members

In C, there are two ways to access the members of a union:

  • Using the dot notation
  • Using the arrow notation

Dot Notation

This method is used when the union is accessed through a variable.

The syntax is:

unionName.memberName

For example:

  • if we have a union named myUnion with a member named myMember, we can access it using myUnion.myMember.

Example

#include <stdio.h>
#include <string.h>

// define a union named myUnion
union myUnion
{
int i;
float f;
char str[20];
};

int main()
{
// create a union variable named u
union myUnion u;

// set the value of the integer member
u.i = 42;

// access the integer member using dot notation
printf("Integer member value: %d\n", u.i);

// set the value of the float member
u.f = 3.14159;

// access the float member using dot notation
printf("Float member value: %f\n", u.f);

// set the value of the string member
strcpy(u.str, "Hello, world!");

// access the string member using dot notation
printf("String member value: %s\n", u.str);

return 0;
}
Output:

Explanation:

  • We define a union named myUnion with three members: an integer i, a float f, and a character array str.
  • We then create a union variable named u and set the value of each member.
  • We access each member using the dot notation, which allows us to access the member that is currently active.
  • Finally, we print the value of each member to the console.

Arrow Notation

This method is used when the union is accessed through a pointer to a variable.

The syntax is:

pointerToUnion->memberName

For example:

  • if we have a pointer to a union named pMyUnion with a member named myMember, we can access it using pMyUnion->myMember.

Example

#include <stdio.h>
#include <string.h>

// define a union named myUnion
union myUnion
{
int i;
float f;
char str[20];
};

int main()
{
// create a pointer to a union variable named u
union myUnion *pu;

// create a union variable named u2
union myUnion u2;

// assign the address of u2 to pu
pu = &u2;

// set the value of the integer member using arrow notation
pu->i = 42;

// access the integer member using arrow notation
printf("Integer member value: %d\n", pu->i);

// set the value of the float member using arrow notation
pu->f = 3.14159;

// access the float member using arrow notation
printf("Float member value: %f\n", pu->f);

// set the value of the string member using arrow notation
strcpy(pu->str, "Hello, world!");

// access the string member using arrow notation
printf("String member value: %s\n", pu->str);

return 0;
}
Output:

Explanation:

  • We define a union named myUnion with three members: an integer i, a float f, and a character array str.
  • We then create a pointer to a union variable named pu and assign the address of a union variable u2 to it.
  • We set the value of each member using the arrow notation, which allows us to access the member that is currently active.
  • Finally, we print the value of each member to the console.
  • Note that in this example, we access the union members through the pointer to the union variable pu.

Comparision

Below table shows the comparison of accessing union members using dot notation and arrow notation:

Dot notationArrow notation
Access through a variableYesNo
Access through a pointerNoYes
Syntaxunion.memberpointer->member
Use caseWhen accessing a union member using a variableWhen accessing a union member using a pointer
ExamplemyUnion.ipMyUnion->f
note

The choice between "dot notation" and "arrow notation" depends on whether the union is accessed through a variable or a pointer, and whether the name of the member is known at compile time or at runtime.