In our previous post you learn Intro to Structures in c programming that is why use structures in C,Syntax of structures in C ,Declaration of structures ,Declaration of structures variable , Accessing structures elements etc.
So Now when you are with Structures in C programming ,let us Explore more program ,power and use of structures in C so that you can take it’s full advantage .

Arrays of Structures

First thing we are exploring in Structures is Arrays Of Structures in C programming.To do this we write a program to store and print employ id,name,branch and salary.Below is the code(Don’t copy and paste the code, type it to make your skills better).

Code:

#include<stdio.h>
int main()
{
struct emp{
    int emp_id;
    char name[20];
    char branch;/*A for computer
              B for mangement*/
    long double sal;
       }a,b;
printf(“Enter empid =”);
scanf(“%d”,&a.emp_id);
printf(“nEnter name = “);
scanf(“%s”,&a.name);
printf(“nEnter emp branch (A for computer and B for mangement) =”);
scanf(“%s”,&a.branch);
printf(“nEnter sal of emp=”);
scanf(“%Lf”,&a.sal);
//================//
printf(“Enter empid =”);
scanf(“%d”,&b.emp_id);
printf(“nEnter name = “);
scanf(“%s”,&b.name);
printf(“nEnter emp branch (A for computer and B for mangement) =”);
scanf(“%s”,&b.branch);
printf(“nEnter sal of emp=”);
scanf(“%Lf”,&b.sal);

//==============//
printf(“nEmp id =%d”,a.emp_id);
printf(“tName is %s”,a.name);
printf(“nBranch–>%c”,a.branch);
printf(“nSal=%Lf”,a.sal);
printf(“nEmp id =%d”,b.emp_id);
printf(“tName is %sn”,b.name);
printf(“nBranch–>%c”,a.branch);
printf(“nSal=%Lfn”,a.sal);
return 0;
}

Output:

arrays of structure :buffercode
arrays of structure

So what we did here is  just declare a structure emp has attributes emp_id,name,branch and his salary ,and then we access these elements using it’s variable. But what if we do this for 100s or 1000s of employees ??
Will we do same??
Is it a efficient way to store and access the value using this method?
Ans:- Off-course No, it’s very difficult to handle 100s of variable.
So what should we do??
Ans:-We use arrays of structures.
So lets do it by code Implementation so that you can understand this concept better.Below is the code for it:-

Code:

#include<stdio.h>
int main()
{
struct emp{
    int emp_id;
    char name[20];
    char branch;/*A for computer
              B for mangement*/
    long double sal;
       }a[10];
int i;
for(i=0;i<10;i++){
printf(“Enter empid %d =”,i);
scanf(“%d”,&a[i].emp_id);
printf(“nEnter name = “);
scanf(“%s”,&a[i].name);
printf(“nEnter emp branch (A for computer and B for mangement) =”);
scanf(“%s”,&a[i].branch);
printf(“nEnter sal of emp=”);
scanf(“%Lf”,&a[i].sal);
 }
for(i=0;i<10;i++){
printf(“nEmp id =%d”,a[i].emp_id);
printf(“tName is %s”,a[i].name);
printf(“nBranch–>%c”,a[i].branch);
printf(“nSal=%Lf”,a[i].sal);
 }
}

What we do here??

In the above code we declare a variable of array of size 10 of structure and store the value of attributes in a for loop as we do in arrays .It save your time and make your program more efficient to use.

Previous