a union is a derived type (similar to structure) with members that share the same storage space. sometimes the same type of construct needs different types of data. used when its necessary to store different types of data in the same storage area.
unions have members. only one member can contain a value at any time so only one access of a member at a given time. a member can have any data type.
unions are useful in embedded programming, situations where direct access to the hardware/memory is needed
the declaration is like structure
union [uniontag] {
type1 identifier1;
type2 identifier2;
...
} optional_vriable_definitions;
example:
union data {
int i;
float f;
char str[20];
}data;
the length of this is same as 'str' variable cause it hase the largest size in the union.
example:
#include <stdio.h>
union car {
int i;
float f;
char c[40];
};
int main(){
union car car1, car2, *car3; // note: car3 is a pointer
printf("memory size occupied by data union %zu\n", sizeof(car1));
return 0;
}
in c11 an unnamed memory union of structure.
example:
#include <stdio.h>
struct owner {
char socsecurity[12];
};
struct leasecompany {
char name[40];
char headquarters[40];
};
struct car_data {
char make[15];
int status;
union { // anonymous uniondefined in a struct with
// no name and cant be user anywhere else
struct owner owncr;
struct leasecompany leasecar;
};
};
exactly like accessing struct members and values
union {
int code;
float cost;
} item;
item.code = 1245;
union {
int code;
float cost;
} item, *ptrst;
ptrst = &item;
ptrst->code = 432;
- ptrst->code
- item.code
- (*ptrst).code
example:
#include <stdio.h>
union mixed {
char c;
float f;
int i;
};
int main(){
union mixed x;
x.c = 'd';
printf("character: %c\n",x.c);
return 0;
}