ENOTES

Differences between array vs structure vs union in C

access_time Jul 21, 2021 remove_red_eye 8976

Arrays are used to represent a group of data items that belong to the same type, such as 'int' for integers or ‘float’ for floating value i.e. decimal numbers. We cannot use an array if we want to represent a collection of data items of several data types using a same name. Which means, if we create an array as int then it cannot hold floating values and strings. This limitation is eradicated  by structure and union in C.

Introduction of Structure in C

A structure is a collection of one or more variables, that may be same or different data types, grouped together under a single(same) name for easy handling.  It is a convenient tool for handling a group of logically related data items.  Structures help to organize complex data in a more meaningful and logical way. Thus, can be used in large program design, because they allow a group of related variables/items to be treated as a single unit. They are also known as constructed datatypes.

[Note: In some language, structures are known as records and the elements of structures are known as fields/members/components.]

Declaration of Structure in C

Keyword used by Structure isstruct

Syntax

struct structure_name
{
	data_type member_1;
	data_type member_2;
	.
	.
	data_type member_n;

}structure_variables;

Let us create a structure to illustrate the above-mentioned declaration,

struct student
{
	char name [35];
	int age;
	char address [50];
	float Percentage;
} s1, s2;

[Note: We can use any number of structure variable depending upon requirement.

Explanation of Structure declaration

  1. The keyword struct introduces a structure declaration with structure name student. It is also called structure tag. Inside it there is a list of declarations enclosed in braces.
  2. The variables name, age, address, percentage declared within the structure declaration are called its structure members.
  3. The struct declaration defines a datatype, and the variables s1 and s2 called structure variables, which follows the struct declaration. Then storage space is reserved.  A structure declaration that is not followed by a list of structure variables reserves no storage space.

 

Introduction of union in C

Union is a user-defined data type. It is a concept borrowed from structures and therefore follow this same syntax structures. However, the major differences between them is in terms of storage. In structures, each member has its own storage location, whereas all the members of a union use the same location.  Which means, although a union may contain many members of different types, it can handle only one member at a time. Every union member takes memory that contains a variety of data item or objects. So that, the union members share space thus helps in conserving memory. The memory size used by a union variable is the size used by the member with the largest size. Since, one memory space is shared by all data members, only one data member can be accessed at a time.

[Note: Use of Union is somehow same as Structure, Its compiler which allocates memory depending upon the keyword ‘struct’ or ‘union’.]

Declaration of Union in C

Keyword used by Union is ‘union’

Syntax

union union_name
{
	data_type member_1;
	data_type member_2;
	.
	.
	data_type member_n;

}union_variable;

Let us create a union to illustrate the above-mentioned declaration,

union student
{
	char name [35];
	int age;
	char address [50];
	float Percentage;
} s1, s2;

[Note: We can use any number of union variable depending upon requirement.

Differences between Array and Structure

ArrayStructure
No special keyword is used to declare array, rather they use index.‘struct’ keyword is used with data members, tags and variables.
Array behaves like a built-in data type.Structure is a user-defined data type.
Array holds the group of same elements under a single name.Structure holds the group of different elements under a single name.
We cannot have array of array.We can have array of structure.
Memory occupied by an array is the multiple of no. of index.Memory occupied structure is sum of all individual data type.
Cannot take part in complex data structureCan take part in complex data structure

Differences between Structure and Union

StructureUnion
The keyword ‘structure’ is used.The keyword ‘union’ is used.
Memory occupied by structure is sum of individual data type.Memory occupied by union is of highest data type of all.
Memory allocation of every element independent.Memory allocation of every element dependent.
If any of the values of any element are changed there is no impact to the other element's values.If any of the values of any element are changed there is direct impact to the other element's values.
Multiples members can be accessed at same time.Only one member can be accessed at same time.
Cannot take part in complex dataCan take part in complex data structure.
Cannot be used to interact with hardware in all aspects.Professionally specialized to interact with hardware.

Program example of array

Program to input 100 number and calculate average of all.

#include<stdio.h>
int main( )
{
 int x[100], i, s=0;
 float a;
 printf(“Enter 100 number”);
 for(i=0;i<3;i++)
 {
	scanf(“%d”, &x[ i ]);
	s=s+x[ i ];
 }
 a=s/100;
 printf(“Average of 100 number is %f”, a);
 return 0;
}

Program to input name, roll number and percentage of student and displaying them using both structure and union

Program example of structure

#include <stdio.h>
int main()
{
    struct student
    {
        char n[10];
        int r;
        float p;
    }s;
    printf("Enter name roll and percentage ");
    scanf("%s %d %f",s.n,&s.r,&s.p);
    printf("Roll=%d || Name=%s || Percentage=%0.2f \n",s.r,s.n,s.p);
    return 0;
}

Program example of union

#include <stdio.h>
int main()
{
    union student
    {
        char n[10];
        int r;
        float p;
    }s;
    printf("Enter name roll and percentage ");
    scanf("%s %d %f",s.n,&s.r,&s.p);
    printf("Roll=%d || Name=%s || Percentage=%0.2f \n",s.r,s.n,s.p);
    return 0;
}