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

Example

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

Illustration of Program

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

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



Short URL: http://tinyit.cc/1e8f8c
Author: Cusp2207 on March 26, 2014
Category: Computer Science
Tags:

Leave a Reply

Last articles