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
myUnionwith a member namedmyMember, we can access it usingmyUnion.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
myUnionwith three members: an integeri, a floatf, and a character arraystr. - We then create a union variable named
uand 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
pMyUnionwith a member namedmyMember, we can access it usingpMyUnion->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
myUnionwith three members: an integeri, a floatf, and a character arraystr. - We then create a pointer to a union variable named
puand assign the address of a union variableu2to 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 notation | Arrow notation | |
|---|---|---|
| Access through a variable | Yes | No |
| Access through a pointer | No | Yes |
| Syntax | union.member | pointer->member |
| Use case | When accessing a union member using a variable | When accessing a union member using a pointer |
| Example | myUnion.i | pMyUnion->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.