Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

STRUCTURE AS MEMBER OF ANOTHER STRUCTURE

A well-defined structure variable can be a member of another structure and even a structure definition can exists inside another structure (inner structure).

UNIONS

Union is another way of creating user-defined data type in C language.   We can create union data type just by using ‘union’ keyword instead of ‘struct’.  Union data type also contains number of data members of basic or user-defined data types.  Each union variable declared out of union data type can access its member using dot (.) operator.  Then what are the differences between structures and unions?  We will look at those interesting points below.

Differences between structure and union

  1. Structure can be defined with ‘struct’ keyword while union can be defined with ‘union’ keyword
  2. The memory size of the variable of structure data type is greater than or equal to the total size of its members size.  The memory size of the variable of union data type is equal to the memory requirement of one data member who need highest memory.
  3. Each data member of structure has individual memory allocation within its variable’s memory.  All data members of union share the same memory allocated for union variable.
  4. Altering value of one data member of structure won’t effect another data member.  Altering value of one data member of union will effect another data members too.
  5. The data members of structure can be accessed at a time but only one member of union can be accessed
  6. Initialisation of more than one member of structure is possible but only the first member of union is possible.

//Example program to show differences between structure and union

#include

typedef struct

{

   char name[10];

   int age;

   float marks;

} stdstruct;

typedef union

{

   char name[10];

   int age;

   float marks;

}stdunion;

int main()

{

stdstruct s1;

stdunion s2;

printf(“Enter values for structure variable:”);

printf(“\nEnter name of the student:”);

scanf(“%s”,s1.name);

printf(“Enter age:”);

scanf(“%d”,&s1.age);

printf(“Enter marks:”);

scanf(“%f”,&s1.marks);

printf(“\n\n”);

printf(“Enter values for union variable:”);

printf(“\nEnter name of the student:”);

scanf(“%s”,s2.name);

printf(“Enter age:”);

scanf(“%d”,&s2.age);

printf(“Enter marks:”);

scanf(“%f”,&s2.marks);

printf(“\n\n”);

printf(“Values for structure variable:”);

printf(“\nName of the student: %s”,s1.name);

printf(“\nAge: %d”,s1.age);

printf(“\nMarks: %f”,s1.marks);

printf(“\n\n”);

printf(“Values for union variable:”);

printf(“\nName of the student: %s”,s2.name);

printf(“\nAge: %d”,s2.age);

printf(“\nMarks: %f”,s2.marks);

printf(“\n\n”);

getch();

s1.age=26;

s2.age=21;

printf(“After modifications, Values for structure variable:”);

printf(“\nName of the student: %s”,s1.name);

printf(“\nAge: %d”,s1.age);

printf(“\nMarks: %f”,s1.marks);

printf(“\n\n”);

printf(“After modifications, Values for union variable:”);

printf(“\nName of the student: %s”,s2.name);

printf(“\nAge: %d”,s2.age);

printf(“\nMarks: %f”,s2.marks);

return(0);

}

You cannot copy content of this page