banner

Structure

              
     In some programming contexts, you need to access multiple data types under a single name for easier data manipulation; for example you want to refer to address with multiple data like house number, street, zip code, country. C supports structure which allows you to wrap one or more variables with different data types. 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.

Defining Structure


struct address
{
int house_number;
char street_name[50];
int zip_code;
char country[50];
};

Declaration of Structure


struct struct_name
{
  structure_member;
  ...
} ins1,ins2, insn;

Or

struct struct_name ins1,ins2, insn;

Example:-

#include<stdio.h>
struct struct_name
{
  int i;
  char a[10];
} ins1,ins2, insn;


void main()
{
ins1.i=10;
strcpy(ins1.a, "Hello");
ins2.i=5;
strcpy(ins2.a, "Hello1");
}
     Here in the above example we create 3 object (ins1,ins2,insn) and store data in 2 (ins1 and ins2).