Tuesday 1 October 2013

Structure in C

String                                                                                                                         Storage classes


Structure:- it is used to combine data type of different nature in one variable.
it is opposite of array. In array we can only store elements of same data type but in structure we can store the elements of different data type. mainly it is used for maintaining records.

for making the structure we must use "struct" keyword.

Ex:-
  struct emp
{
int id;
char name[20];
}emp1 , emp2;
main()
{
printf("enter the id of first emp");
scanf("%d",&emp1.id);
printf("enter the name");
gets(emp1.name);
printf("enter the id of second emp");
scanf("%d",&emp2.id);
printf("enter the name");
gets(emp2.name);
printf("%d",emp1.id);
printf("%d",emp2.id);
puts(emp1.name);
puts(emp2.name);
}

output:-enter the id of first emp : 10
             enter the name : ctutorial
             -enter the id of  second emp : 11
             enter the name : c

10
11
ctutorial
c

Description:-for accessing the members of the structure we use the . operator.
first we create the structure bu using the struct keyword.
then we define two struct type variable emp1 , emp2. which contains separate records of two employee.
then we insert the value . when we write emp1.id it insert the value in  id variable of emp1.
if we write emp2.id it inserts the value in id column of emp2.
same we will do for name variable.
simply in structure we took two variable one id second name.


Use of two Structure:-

Ex:-

struct emp
{
int id;
char name[10];
}
struct empdetail
{
struct emp e; //create variable of first structure type
int age;
}s1,s2; // create variable of second structure type
main()
{
printf("enter the id");
sacnf("%d",&s1.e.id);
printf("enter the name");
gets(s1.e.name);
printf("enter the age");
scanf("%d",&s1.age);
}

Description:- using the members of one structure into the another structure .
first we create the variable of one structure type into another structure.
for accessing the members we must give the full path of the variables in which it presents or declared.
such as ;-s1.e.id;

Structure and Functions:- passing the structure as an arguments to the function.

Ex:-
struct emp
{
int id;
char name[20];
};
void show(struct emp em);
main()
{
struct emp e1;
printf("enter the id");
scanf("%d",&e1.id);
printf("enter the name");
gets(e1.name);
show(e1);
}
void show(struct emp em)
{
printf("%d",em.id);
puts(em.name);
}

Description:- first of all simply we create the structure emp which contains two fields id and name.
now we declare one function show() by which we will print the value.
in the main method we take the value from the user and pass the variable to the function .e1 pass all the details to em. by using em we will print all the attributes of the structure.


Structures with pointer:- accessing the elements of structures by using the pointer we must use "->" operator in place of . operator

Ex:-
struct emp
{
int id;
char name[20];
};
void show(struct emp  *em);
main()
{
struct emp e1;
printf("enter the id");
scanf("%d",&e1.id);
printf("enter the name");
gets(e1.name);
show(&e1);
}
void show(struct emp *em)
{
printf("%d",em->id);
puts(em->name);
}

Description:- in this mainly we are working on address or memory location simply we pass the address of the  structure type variable .and use -> operator for accessing the members.
concepts of call by reference.



No comments :

Post a Comment