structures

Structures in C

http://install4install.com

Structure is a user defined data type which can store related information of different or same data types all together.Whether be it a employee whose information like name,salary,address,contact no  and date of joining is to be stored or whether it be a student whose name,marks,section,contact no,etc is to be stored,structures are an efficient way to store data under single name.It is similar to records and it stores all the related information about an entity.It is declared by using the keyword struct followed by structure name.

Syntax

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

Illustration of above syntax

  • struct is a data type used for declaring variables of different data types.
  • structure_name is the name given to user defined data type.
  • data type relates with different types of data associated with members of structure.
  • It is ended by a semi-colon.

Example

struct employee
{
float salary;
char emp_name[20];
int phone_no;
};

Illustration of Program

  • struct is a keyword used for defining a derived data type (A derived data type employee has been created by the above declaration.Data types that are derived from fundamental data types are called derived data types. Derived data types don’t create a new data type but,instead they add some functionality to the basic data types).
  • salary,emp_name ,phone_no,address are the members of structure of float,char and int  data types respectively.
  • The structure block is terminated by semi colon to indicate the end of structure.

Ways to declare structure variables

  1. Inside main
  2. Outside structure block

Inside main

To declare variables in the main function syntax is as follows

struct structure_name   variable1,variable2,variable2,…..variable n.

Example

struct employee
{
float salary;
char emp_name[20];
int phone_no;
char address[10];
};

int main()
{
//lines of code
struct employee e1,e2,e3[10];
}

e1 and e2 are the variables and e3 is an array of size 10.

Outside structure block

To declare variables outside the structure block  syntax is as follows

struct struct_name
{
//lines of code
}variable1,variable2,////variable n;

Example

struct employee
{
float salary;
char emp_name[20];
int phone_no;
char address[10];
}e1,e2,e3[10];

Accessing members of the structure

Any member of a structure can be accessed using  Member operator(.)The syntax is : structure_variable_name.member_name.

Suppose,we want to access phone_no of variable e2 then we can write it as follows :

e2.phone_no

Example of a structure

#include<stdio.h>
int main()
{
struct employee
{
float salary;
char emp_name[20];
int phone_no;
}e1,e2;
//printf("Enter the salary of first employee");
scanf("%f",&e1.salary);
//printf("Enter the name of first employee");
scanf("%s",&e1.emp_name);
//printf("Enter the phone no first employee");
scanf("%d",&e1.phone_no);
//printf("Enter the salary of second employee");
scanf("%f",&e2.salary);
//printf("Enter the name of second employee");
scanf("%s",&e2.emp_name);
//printf("Enter the phone no second employee");
scanf("%d",&e2.phone_no);
printf("The salary,name and phoneno of first employee is %f %s %d \n",e1.salary,e1.emp_name,e1.phone_no);
printf("The salary,name and phoneno of second employee is %f %s %d",e2.salary,e2.emp_name,e2.phone_no);
}

Run

Illustration of above Program

  • struct is used for defining derived data type called as employee.
  • salary,emp_name and phone_no are the data members of float,char and int type respectively.
  • e1 and e2 are two variables of structure employee declared outside structure block.
  • The values of members of e1 and e2 are taken from user through scanf and displayed through printf.
  • Variable,member operator and member of structure refers to particular value .Like e1.salary where e1 is the variable, . is the member operator and salary is the member which refers to the salary of employee  e1.

Initialization of Structures

struct struct_name
{ 
  datatype  member 1;
  datatype  member 2;
  datatype  member 3;
  ..............................
  datatype member n;
}
struct_var = {constant1, constant2, constant 3,...};

OR

struct struct_name
{ 
  datatype  member 1;
  datatype  member 2;
  datatype  member 3;
  ..............................
  datatype member n;
};
struct struct_name struct_var = {constant1, constant2, };

Example

#include<stdio.h>
struct patient_test
{
int fees;
char name[10];
char result[10];
}t1;
int main()
{
t1.fees = 400;
strcpy(t1.name,"Jack");
strcpy(t1.result,"Positive");
printf("The fees of test is %d\n",t1.fees);
printf("The name of test is %s\n",t1.name);
printf("The result of test is %s\n",t1.result);
return 0;
}

Run

Illustration

  • structure patient_test has been declared with members fees,name and result of int,char and char type respectively.
  • t1 is the variable of patient_test.
  • The character values are initialized  using strcpy function.
  • The values are displayed through printf.

Short URL: http://tinyit.cc/1e8f8c
By Cusp2207 on March 26, 2014 | Computer Science | A comment?
Tags: