Structure


A structure is a bundle of different variables eac h of different type ,we use these structure to define a very new user defined data type, the variables bundeled within the structure are called members of that structures. Further we can us e this new data type to declare variables in a main() body. The size of variables of structure type will be equal to the total size of the member in that structure.

Declaration of a structure


Structure employee structure name
{
char e_name[30];
int e_id,e_sub
};

Different types to initialize a structure-:


  1. via assignment

  2. void main()
    {
    typedef struct employee emp;
    emp e1,e2;
    e1.e_name="abc";
    e1.e_id="abc";
    e1.e_salary="6000";

  3. via scanf()

  4. void main()
    {
    typedef struct employee emp;
    emp e1,e2;
    printf("enter employee name ,id and salary");
    scanf("%s%d%d",e1.e_name,e1.e_id,e1.e_salary);
    ____________________
    ____________________
    ____________________
    ____________________
    }


Program to introduce structure.


#include< stdio.h>
#include< conio.h>
struct student
{
int st_roll;
float st_marks;
char st_name;
};
void main()
{
typedef struct student std;
std s1,s2;
clrscr();
printf("enter roll number name and marks of student ");
scanf("%s%d%f",s1.st_name,&s1.st_roll,&s1.st_marks) ;
printf("you entered ");
printf("name=%sroll number%dmarks%f",s1.st_name,&s1 .st_roll,&s1.st_marks");
getch();
}

Handling of float in array of structure.


#include< stdio.h>
#include< conio.h>
struct book
{
int bid,bnop;
float bprice;
char st_name;
};
void main()
{
typedef struct book bk;
bk b[2];
int i;
for(i=0;i<2;i++)
{
printf("enter id,no of pages and price of book %d", i+1);
scanf("%d%d%f",&b[i].bid&b[i].bnop&b[i].bprice);
}
void link()
{
float a,*p;
p=&a;
a=*p;
}

Accessing structure via pointer.


#include< stdio.h>
#include< conio.h>
struct book
{
int stroll;
float stmarks;
char stname[30];
};
void main()
{
typedef struct student std;
std f1,*p1;
p1=&f1;
clrscr();
printf("enter name roll number name and marks of st udent");
fflush(stdin);
scanf("%s\n"p1->stname);
scanf("%d\n%f",&p1->stroll,&p1->stmarks);
printf("you entered");
printf("name=%s\nroll number=%d\nmarks=%f",p1->stna me,p1->stroll,p1->stmarks);
getch();
}
void link()
{
float a,*p;
p=&a;
a=*p;
}