Introduction to sructure in C programming Language.

Let us assume that we are going to develope a software for a company y to manage all employees information. Employees has employee number,employee name, employee salary, employee job,employee department number and so on. we won't be able to use an array to store all information of the employees given above.
As we discussed earlier tutorials, an array is contiguous memory allocation used to store only similar data type.
From the given fig. problem statement we need to store a employees information in single entity or variable. The employee has different type of attributes having differnt data types like integer ,character,float etc. As the programmer you wish to store this information using C program.
If you are thinking array as the solution it will be very difficult to manage n number of arrays. Where n is total number of array of every attribute of employee e.g name,salary,deptno.
What is the solution for the given above problem?
C language already has the the solution for the same problem.That is structure it is also called the heterogeneous data type.
Using structure we can store the given employee informtion , we can store different data type attributes of employees.

Advantage of Structure in C programming.

Use of structure has several advantages in c programming.
In this article we are going to list all the key advantages.
a. Reduced complexity: Let think of storing employee records of seven different employees with employee number, name,job, salary, bonus and department number like in above example without structure!
What do we need here?
seven integer type variables for storing employee number of seven employees, seven string variables for storing name of seven employees, seven long int type variables for storing salary, seven string variables for storing job of seven employees and seven float type of variable for storing bonus. It's Terrible! right!. Don’t do it that way! .The solution for the problem is structure. That’s it!. Here is simple example for doing that:

  struct employee
  {
    int empno;
    char ename[30];
   char job[30];
   int salary;
   float bonus;
   int deptno;
  };
 struct employee e[7];


b. Heterogeneous collection of data items: Structure can store items with different data types whic allows us to create user defined data-type . For example, if we want to store employees' records with their empno, ename, job,salary,bonus and deptno then it comprises items with different types i.e. empno number can be of integer number type, ename is of string type, job is of string type,salary is of string type and bonus is of floating number type .
In programming C Language, it can be achieved by using structure like this:

  struct employee
  {
    int empno;
   char ename[30];
   char job[30];
   float bonus;
   int salary;
   int deptno;
  };

Here we can create structure variables using struct employee e1, e2 and these variables can store all the details of two employees.
c.Increased productivity: Structure increase the productivity and eliminates lots of burden when dealing with the records which contains heterogeneous data items.
d.Maintainability of code:Using structure Complex types records can be represented by single name.
e.Enhanced code readability: for larger projects readability of the code is crucial. Structure helps the programmer to manage the code very friendly which in turn helps to maintain programs and the project. Using Structure Complex types records can be represented by single name.
f.Mathematical operations: by using primary or basic data-types we can not represent distances (feet-inch system),complex numbers, times (hour-minute-second system). Mathematical operations like subtracting complex numbers, or adding them; finding the difference between two time periods in an hour-minute-second ; adding two distances in a feet-inch systemsystem can be done effectively using the concept of structure.
Here is simple example with complex numbers:
 struct complex
 {
  float real;
  float imag;
 };
 struct complex cmplx1, cmplx2, sum;


Previous Topic:-->> Function Assignments || Next topic:-->>Array Vs Structure.