Structure in C programming language is a user defined data type that groups logically related information of different data types into a single unit. A structure variable can store multiple variables of different data types. Each variable declared in structure is called member.
Generally we want to store more than one information about any object. Suppose we want to store information about all employees of a company. We want to store following information for every employee:
Name (String) Age (integer) Salary (floating point number) Department (String)
We can create a custom data type, which can store all data about a employee under single name.
C programming language provide support for defining a custom data type(structure) which can aggregate and store all related information about an entity. By using a structure, we can store all the details an employee into a structure variable, and we can define an array of structures to store details of all employees.
Defining a Structure in C
Keyword struct is used to declare a structure. Declaration of a structure specifies the format/schema/template of the user defined data type. Like any other variables in C, a structure must be declared, before it's first use in program.
Syntax of Declaring a Structure{
data_type variable_name1;
data_type variable_name2;
.
.
data_type variable_nameN;
};
- struct keyword is used to declare a structure.
- structure_name is the name of the structure.
- data_type variable1; is member variable declaration statement. data_type is the type of member variable.
- We can declare any number of member variables inside a structure.
- Declaring a structure alone will not create any variable or allocate any memory. Memory is allocated first time when a variable of structure type is declared.
Structure declaration to store above mentioned employee details
struct employee { char name[100]; int age; float salary; char department[50]; };
Declaration of Structure Variable
We can declare variable of structure once we defined the format of structure. There are two ways of declaring a structure variable:
- Using struct keyword after structure definition.
- Declaring variables at the time of defining structure.
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one;variable employee_one is an instance of structure employee.
We can declare multiple variables by separating them with comma.
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one, employee_two, employee_three;Declare Variables using struct Keyword
The syntax of defining a structure variable is similar to the variable declaration syntax of any basic data type in C. it uses struct keyword followed by structure name as the data type.
For Example
struct employee employee_one;Above statement declares a variable of structure type employee.
All member variables of a structure are stored in contiguous memory locations.
Initialization of Structure Variable
Like any other variable in C, we can initialize a structure variable at the time of declaration. We can specify initial values as a comma separated list of constants enclosed between curly braces.
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one = {"Jack", 30, 1234.5, "Sales"};We can also initialize structure variable after structure declaration.
struct employee employee_one = {"Jack", 30, 1234.5, "Sales"};
It is not compulsory to initialize all the member variables of a structure. Following statement initializes only name member of structure employee and remaining members gets initialized with zero(for integer and float) and NULL(for pointers and char).
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one = {"Jack"};
Accessing Members of Structure in C
We cannot access members of a structure directly in any expression by specifying their name alone.
There are two ways to access structure membersUsing Member Access Operator(.) or Dot Operator
We can access the member age of employee structure variable employee_one as:
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one = {"Jack", 30, 1234.5, "Sales"}; int age = employee_one.age;
Using Member Structure Pointer Operator or Arrow Operator(->)
Structure pointer operator or Arrow operator is used to access members of structure using pointer variable. When we have pointer to a structure variable, then we can access member variable by using pointer variable followed by an arrow operator and then the name of the member variable.
For Example
struct employee { char name[100]; int age; float salary; char department[50]; } employee_one = {"Jack", 30, 1234.5, "Sales"}; struct employee *ptr = &employee_one; int age = ptr->age;
C Program to print the members of a structure using dot and arrow operators
#include <stdio.h> struct employee { char name[100]; int age; float salary; char department[50]; }; int main(){ struct employee employee_one, *ptr; printf("Enter Name, Age, Salary and Department of Employee\n"); scanf("%s %d %f %s", &employee_one.name, &employee_one.age, &employee_one.salary, &employee_one.department); /* Printing structure members using dot operator */ printf("Employee Details\n"); printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", employee_one.name, employee_one.age, employee_one.salary, employee_one.department); /* Printing structure members using arrow operator */ ptr = &employee_one; printf("\nEmployee Details\n"); printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", ptr->name, ptr->age, ptr->salary, ptr->department); return 0; }Output
Enter Name, Age, Salary and Department of Employee Jack 30 1234.5 Sales Employee Details Name : Jack Age : 30 Salary = 1234.500000 Dept : Sales Employee Details Name : Jack Age : 30 Salary = 1234.500000 Dept : Sales
Passing Structure to Function in C
You can pass a structure variable to a function as argument like we pass any other variable to a function. Structure variable is passed using call by value. We can also pass individual member of a structure as function argument.
C Program to Pass Structure Variable to Function
In below program, we are passing a member variable to printAge function and whole structure variable to printEmployeeDetails function.
#include <stdio.h> struct employee { char name[100]; int age; float salary; char department[50]; }; void printEmployeeDetails(struct employee emp){ printf("Employee Details\n"); printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", emp.name, emp.age, emp.salary, emp.department); } void printAge(int age){ printf("\nAge = %d\n", age); } int main(){ struct employee employee_one, *ptr; printf("Enter Name, Age, Salary and Department of Employee\n"); scanf("%s %d %f %s", &employee_one.name, &employee_one.age, &employee_one.salary, &employee_one.department); /* Passing structure variable to function */ printEmployeeDetails(employee_one); /* Passing structure member to function */ printAge(employee_one.age); return 0; }Output
Enter Name, Age, Salary and Department of Employee Rose 35 1234.5 Purchase Employee Details Name : Rose Age : 35 Salary = 1234.500000 Dept : Purchase Age = 35
Nesting of Structures in C
Nesting of structures is supported in C programming language. We can declare a structure variable as member of structure or write one Structure inside another structure.
There are two ways to define nested structure in C language.
Declaring a Structure Variable as Member of Another Structure
In this approach, we create two structures and include a structure variable as member variable of another structure.
struct Address { int houseNumber; char street[100]; char zipCode; }; struct Employee { char name[100]; int age; float salary; struct Address address; } employee;In above declaration, we have included a variable of structure Address as member of structure Employee.
Declaring a Structure inside Another Structure
In this approach, we declare a structure inside curly braces of another structure.
struct Employee { char name[100]; int age; float salary; struct Address { int houseNumber; char street[100]; char zipCode; } address; } employee;
The normal data members of a structure is accessed by a single dot(.)operator but to access the member of inner structure we have to use dot operator twice.
In above example, we can access zipCode of inner structure as
employee.address.zipCode
C Program to Show Nesting of Structure
In below program, we declare a structure "employee" which contains another structure "address" as member variable.#include <stdio.h> struct employee { char name[100]; int age; float salary; struct address { int houseNumber; char street[100]; }location; }; int main(){ struct employee employee_one, *ptr; printf("Enter Name, Age, Salary of Employee\n"); scanf("%s %d %f", &employee_one.name, &employee_one.age, &employee_one.salary); printf("Enter House Number and Street of Employee\n"); scanf("%d %s", &employee_one.location.houseNumber, &employee_one.location.street); printf("Employee Details\n"); printf(" Name : %s\n Age : %d\n Salary = %f\n House Number : %d\n Street : %s\n", employee_one.name, employee_one.age, employee_one.salary, employee_one.location.houseNumber, employee_one.location.street); return 0; }Output
Enter Name, Age, Salary of Employee Jack 30 1234.5 Enter House Number and Street of Employee 500 Street_One Employee Details Name : Jack Age : 30 Salary = 1234.500000 House Number : 50 Street : Street_One