Nesting Structures and Unions
Program 1: Nested Structures
#include<stdio.h>
typedef struct Small
{
char a;
char b;
int c;
} small;
typedef struct Medium
{
char a;
int c;
char b;
} medium;
typedef struct Big
{
small member1;
medium member2;
} big;
int main(){
big A;
printf("Size of struct big is %d\n",sizeof(big));
printf("Size of struct small is %d\n",sizeof(small));
printf("Size of struct medium is %d\n\n",sizeof(medium));
printf("Size of A is %d\n",sizeof(A));
printf("Size of member1 of A is %d\n",sizeof(A.member1));
printf("Size of member2 of A is %d\n\n",sizeof(A.member2));
printf("Address of A is %lu\n",&A);
printf("\nchar a Inside instance A member1: %u",&A.member1.a);
printf("\nchar b Inside instance A member1: %u",&A.member1.b);
printf("\nint c Inside instance A member1: %u",&A.member1.c);
printf("\n");
printf("\nchar a Inside instance A member2: %u",&A.member2.a);
printf("\nint c Inside instance A member2: %u",&A.member2.c);
printf("\nchar b Inside instance A member2: %u",&A.member2.b);
}
Output:

Program 2: structures inside union
#include<stdio.h>
typedef struct Small
{
char a;
char b;
int c;
} small;
typedef struct Medium
{
char a;
int c;
char b;
} medium;
typedef union Big
{
small member1;
medium member2;
} big;
int main(){
big A;
printf("Size of union big is %d\n",sizeof(big));
printf("Size of struct small is %d\n",sizeof(small));
printf("Size of struct medium is %d\n\n",sizeof(medium));
printf("Size of union big instance A is %d\n",sizeof(A));
printf("Size of member1 of A is %d\n",sizeof(A.member1));
printf("Size of member2 of A is %d\n\n",sizeof(A.member2));
printf("Address of A is %lu\n",&A);
printf("\nchar a Inside instance A member1: %u",&A.member1.a);
printf("\nchar b Inside instance A member1: %u",&A.member1.b);
printf("\nint c Inside instance A member1: %u",&A.member1.c);
printf("\n");
printf("\nchar a Inside instance A member2: %u",&A.member2.a);
printf("\nint c Inside instance A member2: %u",&A.member2.c);
printf("\nchar b Inside instance A member2: %u",&A.member2.b);
}
Output:

Program 3: Unions inside structure
#include<stdio.h>
typedef union Small
{
char a;
char b;
int c;
} small;
typedef union Medium
{
char a;
int c;
char b;
} medium;
typedef struct Big
{
small member1;
medium member2;
} big;
int main(){
big A;
printf("Size of struct big is %d\n",sizeof(big));
printf("Size of union small is %d\n",sizeof(small));
printf("Size of union medium is %d\n\n",sizeof(medium));
printf("Size of struct big instance A is %d\n",sizeof(A));
printf("Size of member1 of A is %d\n",sizeof(A.member1));
printf("Size of member2 of A is %d\n\n",sizeof(A.member2));
printf("Address of A is %lu\n",&A);
printf("\nchar a Inside instance A member1: %u",&A.member1.a);
printf("\nchar b Inside instance A member1: %u",&A.member1.b);
printf("\nint c Inside instance A member1: %u",&A.member1.c);
printf("\n");
printf("\nchar a Inside instance A member2: %u",&A.member2.a);
printf("\nint c Inside instance A member2: %u",&A.member2.c);
printf("\nchar b Inside instance A member2: %u",&A.member2.b);
}
Output:

Program 4: Union inside union
#include<stdio.h>
typedef union Small
{
char a;
char b;
int c;
} small;
typedef union Medium
{
char a;
int c;
char b;
} medium;
typedef union Big
{
small member1;
medium member2;
} big;
int main(){
big A;
printf("Size of union big is %d\n",sizeof(big));
printf("Size of union small is %d\n",sizeof(small));
printf("Size of union medium is %d\n\n",sizeof(medium));
printf("Size of union big instance A is %d\n",sizeof(A));
printf("Size of member1 of A is %d\n",sizeof(A.member1));
printf("Size of member2 of A is %d\n\n",sizeof(A.member2));
printf("Address of A is %lu\n",&A);
printf("\nchar a Inside instance A member1: %u",&A.member1.a);
printf("\nchar b Inside instance A member1: %u",&A.member1.b);
printf("\nint c Inside instance A member1: %u",&A.member1.c);
printf("\n");
printf("\nchar a Inside instance A member2: %u",&A.member2.a);
printf("\nint c Inside instance A member2: %u",&A.member2.c);
printf("\nchar b Inside instance A member2: %u",&A.member2.b);
}
Output:

Program 5: Another example of a union inside a structure
#include <stdio.h>
#include <string.h>
union id_proof
{
char pan_id[11];
char voter_id[11];
long long int aadhaar_num;
char passport_num[9];
};
struct person
{
char name[25];
int age;
char address[50];
union id_proof identity_proof; // union variable is now the member of the structure
};
int main()
{
struct person acct_holder; // structure variable declaration
// assigning the values to the members of the structure variable acct_holder
strcpy(acct_holder.identity_proof.pan_id, "ABCDE1217F");
strcpy(acct_holder.name, "Rahul");
acct_holder.age=39;
strcpy(acct_holder.address, "Mumbai");
// displaying the values of the members of the structure
printf(" Name of the account holder : %s \n", acct_holder.name);
printf(" Age of the account holder : %d \n", acct_holder.age);
printf(" Address of the account holder : %s \n", acct_holder.address);
printf(" Identity Proof number of the holder : %s \n", acct_holder.identity_proof.pan_id);
return 0;
}
Output:


