Friday, July 6, 2018

Structure in C

Structure in c language is a user defined datatype that allows you to hold different type of elements.

Each element of a structure is called a member.

It is widely used to store student information, employee information, product information, book information etc.

Defining structure

The struct keyword is used to define structure. Let's see the syntax to define structure in c.





struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};




Let's see the example to define structure for employee in c.





struct employee
{ int id;
char name[50];
float salary;
};



Here, struct is the keyword, employee is the tag name of structure; id, name and salary are the members or fields of the structure. Let's understand it by the diagram given below:

c structure

Declaring structure variable

We can declare variable for the structure, so that we can access the member of structure easily. There are two ways to declare structure variable:

  1. By struct keyword within main() function
  2. By declaring variable at the time of defining structure.

1st way:

Let's see the example to declare structure variable by struct keyword. It should be declared within the main function.





struct employee
{ int id;
char name[50];
float salary;
};



Now write given code inside the main() function.





struct employee e1, e2;



2nd way:

Let's see another way to declare variable at the time of defining structure.





struct employee
{ int id;
char name[50];
float salary;
}e1,e2;



Which approach is good

But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the structure variable many times.

If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main() fuction.

Accessing members of structure

There are two ways to access structure members:

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)

Let's see the code to access the id member of p1 variable by . (member) operator.





p1.id



C Structure example

Let's see a simple example of structure in C language.





#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=131;
strcpy(e1.name, "Rama");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}



Output:

employee 1 id : 131

employee 1 name : Rama

Let's see another example of structure in C language to store many employees information.





#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=131;
strcpy(e1.name, "Rama");//copying string into char array
e1.salary=70000;

//store second employee information
e2.id=136;
strcpy(e2.name, "Pavi");
e2.salary=150000;

//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
printf( "employee 1 salary : %f\n", e1.salary);

//printing second employee information
printf( "employee 2 id : %d\n", e2.id);
printf( "employee 2 name : %s\n", e2.name);
printf( "employee 2 salary : %f\n", e2.salary);
return 0;
}



Output:

employee 1 id : 131

employee 1 name : Rama

employee 1 salary : 70000.000000

employee 2 id : 136

employee 2 name : Pavi

employee 2 salary : 150000.000000




Instagram



No comments:

Post a Comment