as

Thursday 13 November 2014

Structure in C


So far we have used arrays to store data. But there is one drawback in arrays. We can store only data of similar type in arrays.
For example – suppose you want to store the data of employee like name, id, address etc. It is not possible to store this data in same array as data type of the id is integer and data type of name id character.
This is when structures come into picture.
In below image, we have a structure to store data for ID and Name. Total memory required by below structure is 8 bytes.



struct employee
{
int no;
char name[10];
int mob;
char Address[50];
}

Simple structure example

#include<stdio.h>             

void main()
{
  struct dept      //declaring structure
   {
    int id;
    char name[15];
  };
   struct dept d={01,"Production"};              //Initializing structure elements
   printf("\n id=%d",d.id);                            //displaying value
   printf("\n name=%s",d.name);
 getchar();
}








No comments:

Post a Comment

Leave your valuable feedback. Your thoughts do matter to us.

Sponsored Links

Popular Posts

Comments

ShareThis