« Previous                                                                 Next Chapter »
Structure

In some scenarios, we need to access multiple data types under a single name for easier data manipulation. For this, C supports structure which allows  us to include one or more variables with different data types. 

Structure is user defined data type which is used to store heterogeneous data under unique name. A structure can contain any valid data types like int, char, float even arrays or even other structures. Each variable in structure is called a structure member. Keyword 'struct' is used to declare structure.

Syntax:

struct structure_name

{

  <data-type> element 1;

  <data-type> element 2;

    .

    .

  <data-type> element n;

}struct_var;

Ex:

struct emp_info

{

  char emp_id[10];

  char nm[100];

  float sal;

}emp; 

Note:

  • Structure is always terminated with semicolon (;).
  • Structure name as emp_info can be later used to declare structure variables of its type in a program.

Instances of structure

Instances of structure can be created in two ways as,

Instance 1:

struct emp_info

{

  char emp_id[10];

  char nm[100];

  float sal;

}emp;

Instance 2:

struct emp_info

{

  char emp_id[10];

  char nm[100];

  float sal;

};

struct emp_info emp;

In above example, emp_info is a simple structure which consists of stucture members as Employee ID(emp_id), Employee Name(nm), Employee Salary(sal).


Accessing structure members

Structure members can be accessed using member operator '.' . It is also called as 'dot operator' or 'period operator'.

Syntax: structure_var.member;

Ex: emp.emp_id;

If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure.

Ex: struct  emp ravi;

      ravi.billing_addr.country; 


Initializing structure

C programming language treats a structure as a custom data type therefore we can initialize a structure like a variable.

Ex:

struct product

   {

      char name[50];

      double price;

    } book = { "C programming language",40.5};

In above example, we define product structure, then we declare and initialize book structure with its name and price.


Structure with typedef keyword

To make our program more simple, we can use typedef keyword to create a synonym for a structure. This is an example of using typedef keyword to define address structure so when we want to create an instance of it we can omit the keyword struct

typedef struct

    {

          unsigned int house_number;

          char street_name[50];

          int zip_code;

          char country[50];

    } address;

    address billing_addr;

    address shipping_addr;


Structure and sizeof function

sizeof is used to get the size of any data types even with any structures.

Ex:

#include <stdio.h>

typedef struct _address

{

    int house_number;  // 4 bytes

    char street[50];       // 50 bytes

    int zip_code;           // 4 bytes

    char country[20];    // 20 bytes

} address;                   // 78 bytes in total 

void main()

{

    // it returns 80 bytes

    printf("size of address is %d bytes\n",sizeof(address));

We will never get the size of a structure exactly as we think it must be. The sizeof function returns the size of structure larger than it is because the compiler pads struct members so that each one can be accessed faster without delays.

 

More Examples
       /* program to enter and print the employee details */
#include<stdio.h>
#include<conio.h>
struct
{
int eno;
char ename[40];
float sal;
}x;
main()
{
clrscr();
printf("enter the eno,ename,sal");
scanf("%d %s %f",&x.eno,&x.ename,&x.sal);
printf("%d %s %f",x.eno,x.ename,x.sal);
}

/* second logic */
#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char ename[40];
float sal;
};
main()
{
struct emp x;
clrscr();
printf("enter the eno,ename,sal");
scanf("%d %s %f",&x.eno,&x.ename,&x.sal);
printf("%d %s %f",x.eno,x.ename,x.sal);
}

/* third logic */
#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char ename[40];
float sal;
};
typedef struct emp ravi;
main()
{
ravi x;
clrscr();
printf("enter the eno,ename,sal");
scanf("%d %s %f",&x.eno,&x.ename,&x.sal);
printf("%d %s %f",x.eno,x.ename,x.sal);
}

/* fourth logic */
#include<stdio.h>
#include<conio.h>
typedef struct emp
{
int eno;
char ename[40];
float sal;
}ravi;
main()
{
ravi x;
clrscr();
printf("enter the eno,ename,sal");
scanf("%d %s %f",&x.eno,&x.ename,&x.sal);
printf("%d %s %f",x.eno,x.ename,x.sal);
}

/* program to read and print book details */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
float price;
};
main()
{
struct book x;
clrscr();
printf("enter the bno,bname,price");
scanf("%d %s %f",&x.bno,&x.bname,&x.price);
printf("%d %s %f",x.bno,x.bname,x.price);
}

/* program to read and print train details */
#include<stdio.h>
#include<conio.h>
struct train
{
int tno;
char tname[40];
float fare;
};
main()
{
struct train t;
clrscr();
printf("enter the tno,tname,fare");
scanf("%d %s %f",&t.tno,&t.tname,&t.fare);
printf("%d %s %f",t.tno,t.tname,t.fare);
}

/* program to read and print student details */
#include<stdio.h>
#include<conio.h>
struct student
{
int sno;
char sname[40];
};
main()
{
struct student s;
clrscr();
printf("enter the sno,sname");
scanf("%d %s",&s.sno,&s.sname);
printf("%d %s",s.sno,s.sname);
}

/* program to find the price of the book by using structure */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno,nop;
char bname[40];
float price;
};
main()
{
struct book b;
clrscr();
printf("enter the bno,bname,nop");
scanf("%d %s %d",&b.bno,&b.bname,&b.nop);
b.price=b.nop*0.25;
printf("book no=%d\n book name=%s\n price=%f\n", b.bno,b.bname,b.price);
}

/* program to read and print train details */
#include<stdio.h>
#include<conio.h>
struct train
{
int tno,nok;
char tname[40];
float fare;
};
main()
{
struct train t;
clrscr();
printf("enter the tno,tname,nok");
scanf("%d %s %d",&t.tno,&t.tname,&t.nok);
t.fare=t.nok*1.25;
printf("train no=%d\n train name=%s\n fare=%f\n", t.tno,t.tname,t.fare);
}

/* program to find the avg of the player */
#include<stdio.h>
#include<conio.h>
struct player
{
int pid,nom,nor;
char pname[40];
float avg;
};
main()
{
struct player p;
clrscr();
printf("enter the pid,pname,nom,nor");
scanf("%d %s %d %d",&p.pid,&p.pname,&p.nom,&p.nor);
p.avg=(float)p.nor/p.nom;
printf("player ID=%d\n player name=%s\n matches=%d\n avg=%f\n", p.pid,p.pname,p.nom,p.avg);
}

/* program to read and print student details */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int sno,m1,m2,m3,tot;
char sname[40],status[40];
float avg;
};
main()
{
struct student s;
clrscr();
printf("enter the sno,sname,m1,m2,m3");
scanf("%d %s %d %d %d",&s.sno,&s.sname,&s.m1,&s.m2,&s.m3);
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3.0;
if((s.m1>=35)&&(s.m2>=35)&&(s.m3>=35))
{
if(s.avg>=60)
strcpy(s.status,"***FIRST CLASS***");
else if(s.avg>=50)
strcpy(s.status,"***SECOND CLASS***");
else if(s.avg>=35)
strcpy(s.status,"***THISRD CLASS***");
else
strcpy(s.status,"***FAIL***");
}
else
strcpy(s.status,"***FAIL***");
printf(" student no=%d\n student name=%s\n total=%d\n avg=%f\n status=%s\n",s.sno,s.sname,s.tot,s.avg,s.status);
}

/*program to read and print group of students by using structure array*/
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
};
main()
{
struct book b[10];
int i,n;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the bno,bname");
scanf("%d %s",&b[i].bno,&b[i].bname);
}
for(i=0;i<n;i++)
printf(" %d %s\n",b[i].bno,b[i].bname);
}

/* program to read and print book details */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno,nop;
char bname[40];
float price;
};
main()
{
struct book b[40];
int i,n;
clrscr();
printf("enter the no of variables to be used");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the bno,bname,nop");
scanf("%d %s %d",&b[i].bno,&b[i].bname,&b[i].nop);
b[i].price=b[i].nop*0.25;
}
printf("\n");
for(i=0;i<n;i++)
printf(" %d %s %f\n",b[i].bno,b[i].bname,b[i].price);
}

/* example for nested structure */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct marks
{
int m1,m2,m3;
};
struct student
{
int sno,tot;
char sname[40],status[40];
float avg;
struct marks m;
};
main()
{
struct student s;
clrscr();
printf("enter the sno,sname,m1,m2,m3");
scanf("%d %s %d %d %d", &s.sno,&s.sname,&s.m.m1,&s.m.m2,&s.m.m3);
s.tot=s.m.m1+s.m.m2+s.m.m3;
s.avg=s.tot/3.0;
if((s.m.m1>=35)&&(s.m.m2>=35)&&(s.m.m3>=35))
{
if(s.avg>=60)
strcpy(s.status,"***FIRST CLASS***");
else if(s.avg>=50)
strcpy(s.status,"***SECOND CLASS***");
else if(s.avg>=35)
strcpy(s.status,"***THISRD CLASS***");
else
strcpy(s.status,"***FAIL***");
}
else
strcpy(s.status,"***FAIL***");
printf(" student no=%d\n student name=%s\n total=%d\n avg=%f\n status=%s\n", s.sno,s.sname,s.tot,s.avg,s.status);
}


  « Previous                                                                  Next Chapter »