Sunday, 11 December 2011

Structures


Derived types- 
structures- declaration, 
definition and initialization of structures, 
accessing structures, 
nested structures, 
Array of Structures
structures and functions, 
pointers to structures, 
self referential structures, 
Unions
typedef
Bit fields

Definition:
Structure is a collection of different data types. Each data is called its member.

1. Difference between Array and Structures

Structure
Array
1.       It is a collection of elements(variables) of different data types
1.       It is a collection of elements of same data type
2.       We have to use .(dot) operator or -> (cap) operator for accessing
2.       Subscript for accessing array elements
3.       Syntax:
struct  tag
{
  Datatype memeber1;
  Datatype member2;
  …….
};
3.       Declaration of array:
Datatype arrayname[size]={list of elements separated by comma};
4.       Example:
struct  student
{
 int rollno;
 char name[10];
 float avg;
};
Struct student stud={01,”raj\0”,90.25};
4.       int a[5]={1,2,3,4,5};


#include<conio.h>
#include<iostream.h>
struct student
{
char name[10];
int regno;
struct dob;
{
Int day,month,year;
}d;
Int m1,m2,m3,m4,a1,a2,a3,a4;
}s[10];
Void main();
{
Int I,j,n,total,avg;
Clrscr();
Printf(“enter the total no of students”);
Scanf(“%d”,&n);
For(i=0;i<n;i++)
{
printf(“enter the name of the student”);
scanf(“%s”,&s[i].name);
printf(“enter regno”);
scanf(“%d”,&s[i].regno);
printf(“enter the dob of student(dd\mm\yy):t”);
scanf(“%d%d%d,&s[i].d.day,&s[i].d.month,&s[i].d.year);
printf(“enter the marks in c language”);
scanf(“%d”,&s[i].m1);
printf(“enter the marks in physics”);
scanf(“%d”,&s[i].m2);
printf(“enter the marks in civil”);
scanf(“%d”,&s[i].m3);
printf(“enter the marks in mechanics”);
scanf(“%d”,&s[i].m4);
total=s[1].m1+s[i].m2+s[i].m3+s[i].m4;
avg=total/4;
printf(“the total and average of student is %d%d,total,avg);
}
Printf(“subject average\n”);
For(i=0;i<n;i++)
{
a1+=s[i].m1;
Sa1=a1/n;
a2+=s[i].m2;
Sa2=a2/n;
A3+=s[i].m3;
Sa3=a3/n;
A4+=s[i].m4;
Sa4=a4/n;
}
Printf(“%d”,sa1);
Printf(“%d”,sa2);
Printf(“%d”,sa3);
Printf(“%d”,sa4);
Getch();
}

Nested Structures: Structure in a structure is known as Nested Structures


//Program on Nested Structure
#include "stdio.h"
#include "conio.h"
struct adress {
char ct[10];
char dist[10],state[5];
long int pin;
};
struct emp 
{
 char name[10];
 int age,sal;
 struct adress a;
};
void main()
{
 struct emp e[2];
 int i;
 clrscr();
 for(i=0;i<=1;i++)
 {
   printf("Enter [%d]st Employee's Name,Age,salary :: ", i);
   fflush(stdin);
   gets(e[i].name);

   scanf("%d%d",&e[i].age,&e[i].sal);
   printf("\nEnter city, district,state & pincode ::");
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   scanf("%ld",&e[i].a.pin);
}

for(i=0;i<=1;i++)
{
printf("\n[%d]st Employee's Name :: %s",i,e[i].name);
printf("\n[%d]st Employee's Age :: %d ",i,e[i].age);
printf("\n[%d]st Employee's Salary :: %d",i,e[i].sal);
printf("\n[%d]st Employee's City :: %s ",i,e[i].a.ct);
printf("\n[%d]st Employee's District :: %s",i,e[i].a.dist);
printf("\n[%d]st Employee's State :: %s",i,e[i].a.state);
printf("\n[%d]st Employee's Pin :: %ld",i,e[i].a.pin);
}
getch();
}


2)
#include <stdio.h>
#include <conio.h>

struct stud_Res
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}result;

void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&result.rno);
printf("\n\t Enter Standard : ");
scanf("%s",result.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",result.rno);
printf("\n\n\t Standard : %s",result.std);
printf("\nSubject Code : %s",result.marks.subj_nm);
printf("\n\n\t Marks : %d",result.marks.subj_mark);
getch();
}


#include<stdio.h>
#include<conio.h>
typedef struct stud
{
 int pino;
 char ename[10];
 float avg;
};
void main()
{
 stud s;
 clrscr();
 s.pino=20;
 printf("%d\n%s\n%.2f",s.pino,s.ename,s.avg);

 getch();

}


#include<stdio.h>
#include<conio.h>
typedef struct emp
{
 int empno;
 char ename[20];
 char gender;
 float sal;
};
void main()
{
// struct emp e;
 emp e;
// typedef struct emp e;
 printf("enter the empno,ename,gender,sal\n");
 scanf("%d",&e.empno);
 fflush(stdin);
 gets(e.ename);
 fflush(stdin);
 scanf("%c%f",&e.gender,&e.sal);
 printf("employees are\n");
 printf("%d\n%s\n%c\n%.2f\n",e.empno,e.ename,e.gender,e.sal);
 getch();
}


#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct admission
{
 char name[20];
 int rank;
};
void main()
{
 int x;
 static struct admission s1={"rama",681};
 static struct admission s2;
 s2=s1;
 printf("contents of s2 are\n");
 printf("%s %d",s2.name,s2.rank);
 if((strcmp(s1.name,s2.name)==0)&(strcmp(s1.rank,s2.rank)==0)
 printf("both are same\n");
 else
 printf("both are not same\n");
 getch();
}
//Program on Nested Structure
#include "stdio.h"
#include "conio.h"
struct adress {
char ct[10];
char dist[10],state[5];
long int pin;
};
struct emp 
{
 char name[10];
 int age,sal;
 struct adress a;
};
void main()
{
 struct emp e[2];
 int i;
 clrscr();
 for(i=0;i<=1;i++)
 {
   printf("Enter [%d]st Employee's Name,Age,salary :: ", i);
   fflush(stdin);
   gets(e[i].name);
 //  scanf("%s%d%d",e[i].name,&e[i].age,&e[i].sal);
   scanf("%d%d",&e[i].age,&e[i].sal);
   printf("\nEnter city, district,state & pincode ::");
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   fflush(stdin);
   gets(e[i].name);
   scanf("%ld",&e[i].a.pin);
}

for(i=0;i<=1;i++)
{
printf("\n[%d]st Employee's Name :: %s",i,e[i].name);
printf("\n[%d]st Employee's Age :: %d ",i,e[i].age);
printf("\n[%d]st Employee's Salary :: %d",i,e[i].sal);
printf("\n[%d]st Employee's City :: %s ",i,e[i].a.ct);
printf("\n[%d]st Employee's District :: %s",i,e[i].a.dist);
printf("\n[%d]st Employee's State :: %s",i,e[i].a.state);
printf("\n[%d]st Employee's Pin :: %ld",i,e[i].a.pin);
}
getch();
}

You may like the following posts:

No comments:

Post a Comment