C examples for Data Type:union
structures reserve separate memory segments for each member, a union reserves a single memory space for its largest member.
Unions are created with the keyword union.
The following code creates a union definition for a phone book.
union phoneBook { char *name; char *number; char *address; };
union members are accessed via the dot operator.
#include <stdio.h> union phoneBook { char *name; /*from w w w .j a v a 2s . c o m*/ char *number; char *address; }; struct magazine { char *name; char *author; int isbn; }; int main() { union phoneBook aBook; struct magazine aMagazine; printf("\nUnion Details\n"); printf("Address for aBook.name: %p\n", &aBook.name); printf("Address for aBook.number: %p\n", &aBook.number); printf("Address for aBook.address: %p\n", &aBook.address); printf("\nStructure Details\n"); printf("Address for aMagazine.name: %p\n", &aMagazine.name); printf("Address for aMagazine.author: %p\n", &aMagazine.author); printf("Address for aMagazine.isbn: %p\n", &aMagazine.isbn); } //end main