Structures C Programming
What is Structure and its need?
Structure is a collection of variables (variables can be of different types) under a single
name.
For example:
We may want to store information about a person: his/her name, citizenship number and
salary. We can create different variables name, citNo and salary to store this information
separately.
[null img]
Its need: What if we need to store information of more than one person? Now, we need to
create different variables for each information per person:
i.e.
name1, citNo1, salary1, name2, citNo2, salary2…….etc.
A better approach would be to have a collection of all related information under a single
name Person as a structure, and use it for every person.
How to define a structure in C program?
Keyword struct is used in C code for creating a structure.
[null img]
struct structure_name
data_type member-1;
data_type member-2;
data_type member-n;
};Syntax of structure:
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Above declaration creates the derived data type (or User defined type) struct Person. Once
derived data type like Person is created, then we can create variables of that type. I.e.
Person p1,p2,…,pn or p[n] as below
Creating structure variables:
When a structure is defined, it creates a user-defined type. However, no storage or memory is
allocated.
Here' how structure variables created:
Eg:-struct Person
char name[50];
int citNo;
float salary;
};
int main()
struct Person person1, person2, p[20];
return 0;
Another way of creating a structure variable is:
struct Person
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
In both cases, two variables person1, person2, and an array variable p having 20 elements of
type struct Person are created.
How to Access members of a structure?
There are two types of operators used for accessing members of a structure.
1. Member operator(.)
2. Structure pointer operator(->)
Eg:- Suppose, we want to access salary of person2. Here's how you can do
it:
person2.salary;
Example: C structure
// Program to add two distances which is in feet and inches
#include <stdio.h>
struct Distance
int feet;
float inch;
} dist1, dist2, sum;
int main()
printf("1st distance\n");
printf("Enter feet and inch: ");
scanf("%d%f", &dist1.feet, &dist1.inch);
printf("2nd distance\n");
printf("Enter feet and inch: ");
scanf("%d%f", &dist2.feet, &dist2.inch);
// adding feet
sum.feet = dist1.feet + dist2.feet;
// adding inches
sum.inch = dist1.inch + dist2.inch;
// changing feet if inch is greater than 12
while (sum.inch >= 12)
++sum.feet;
sum.inch = sum.inch - 12;
printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
return 0;
Output
1st distance
Enter feet: 12
Enter inch: 7.9
2nd distance
Enter feet: 2
Enter inch: 9.8
Sum of distances = 15'-5.7"
-------------------------------------------------------------------------------------
Nested Structure in C
When a structure contains another structure, it is called nested structure.
“For example, we can have two structures named Address and Employee. To make Address
nested to Employee, we have to define Address structure before and outside Employee
structure and create a variable of structure type i.e. Address type inside Employee
structure.”
Syntax for structure within structure or nested structure
struct structure1
----------
----------
};
struct structure2
----------
----------
struct structure1 st;
};
Example for structure within structure or nested structure
#include<stdio.h>
[null img][null img] struct Address
char HouseNo[25];
char City[25];
char PinCode[25];
[null img] }; Example of Nested Structure
struct Employee
int Id;
char Name[25];
float Salary;
struct Address Add;
[null img] };
void main()
{
struct Employee E;
printf("\n\tEnter Employee Id : ");
scanf("%d",&E.Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&E.Name);
printf("\n\tEnter Employee Salary : ");
scanf("%f",&E.Salary);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.HouseNo);
printf("\n\tEnter Employee City : ");
scanf("%s",&E.Add.City);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.PinCode);
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
Output :
Enter Employee Id : 101
Enter Employee Name : Ritesh
Enter Employee Salary : 45000
Enter Employee House No : 4598/D
Enter Employee City : Delhi
Enter Employee Pin Code : 110056
Details of Employees
Employee Id : 101
Employee Name : Ritesh
Employee Salary : 45000
Employee House No : 4598/D
Employee City : Delhi
Employee Pin Code : 110056
Passing structure as an argument to a function
A structure variable can be passed to the function as an argument as a normal variable.
If structure is passed by value, changes made to the structure variable inside the function
definition does not reflect in the originally passed structure variable.
#include <stdio.h>
#include <string.h>
struct student
char name[50];
int roll;
};
void display(struct student stu);
int main()
struct student stud;
printf("Enter student's name: ");
gets(stud.name);
printf("Enter roll number:");
scanf("%d", &stud.roll);
display(stud); // passing structure variable stud as argument
return 0;
void display(struct student stu){
printf("Output\nName: %s",stu.name);
printf("\nRoll: %d",stu.roll);
Output
Enter student's name: Kevin Amla
Enter roll number: 149
Output
Name: Kevin Amla
Roll: 149
Here's how we can return structure from a function:
#include <stdio.h>
struct student
char name[50];
int age;
};
// function prototype
struct student getInformation();
int main()
struct student s;
s = getInformation();
printf("\nDisplaying information\n");
printf("Name: %s", s.name);
printf("\nRoll: %d", s.age);
return 0;
struct student getInformation()
struct student s1;
printf("Enter name:");
scanf ("%[^\n]%*c", s1.name);
printf("Enter age:");
scanf("%d", &s1.age);
return s1;
Passing structure by reference
You can also pass a structure by reference (in a similar way like you pass variables of built-in
type by reference). We suggest you to read pass by reference article before you proceed.
During pass by reference, the memory address of a structure is passed to the function.
#include <stdio.h>
typedef struct Complex
float real;
float imag;
} complex;
void addNumbers(complex c1, complex c2, complex *result);
int main()
{
complex c1, c2, result;
printf("For first number,\n");
printf("Enter real part: ");
scanf("%f", &c1.real);
printf("Enter imaginary part: ");
scanf("%f", &c1.imag);
printf("For second number, \n");
printf("Enter real part: ");
scanf("%f", &c2.real);
printf("Enter imaginary part: ");
scanf("%f", &c2.imag);
addNumbers(c1, c2, &result);
printf("\nresult.real = %.1f\n", result.real);
printf("result.imag = %.1f", result.imag);
return 0;
void addNumbers(complex c1, complex c2, complex *result)
result->real = c1.real + c2.real;
result->imag = c1.imag + c2.imag;
When you run the program, the output will be:
For first number,
Enter real part: 1.1
Enter imaginary part: -2.4
For second number,
Enter real part: 3.4
Enter imaginary part: -3.2
result.real = 4.5
result.imag = -5.6
Array of Structures in C
Declaring an array of structure is same as declaring an array of fundamental types.
Since an array is a collection of elements of the same type. “In an array of structures, each
element of an array is of the structure type.”
Syntax:
struct struct-name
datatype var1;
datatype var2;
----------
----------
datatype varN;
};
Void main()
struct struct-name Structure_Variabke_Name[ size ];//Array of structure
-----------
-----------
Example: below code is for getting and displaying the data of 3 employee using array of
structure.
#include<stdio.h>
struct Employee
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
int i;
struct Employee Emp[ 3 ]; //Array of structure statement
for(i=0;i<3;i++)
{
printf("\nEnter details of %d Employee",i+1);
printf("\n\tEnter Employee Id : ");
scanf("%d",&Emp[i].Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&Emp[i].Name);
printf("\n\tEnter Employee Age : ");
scanf("%d",&Emp[i].Age);
printf("\n\tEnter Employee Salary : ");
scanf("%ld",&Emp[i].Salary);
printf("\nDetails of Employees");
for(i=0;i<3;i++)
printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);
}
Output :
Enter details of 1 Employee
Enter Employee Id : 101
Enter Employee Name : Suresh
Enter Employee Age : 42
Enter Employee Salary : 52000
Enter details of 2 Employee
Enter Employee Id : 102
Enter Employee Name : Mukesh
Enter Employee Age : 31
Enter Employee Salary : 50000
Enter details of 3 Employee
Enter Employee Id : 103
Enter Employee Name : Ramesh
Enter Employee Age : 28
Enter Employee Salary : 47000
Details of Employees
101 Suresh 42 52000
102 Mukesh 31 50000
103 Ramesh 28 47000