Structure in C

A structure in c programming is a collection of one or more variables of different types. The structure in c tutorial will teach you how to declare a structure, an array of structure, pointer structure, nested structure and how to access an element of a structure.

Structure: Key Points

  • structure can store more than one values but of different data types.
  • It is also known as user defined data type in c.
  • struct keyword is used to declare structure in c.
  • variables inside the structure are called member of structure.
  • we can access members with the help of object of structure.

The syntax of a Structure.

struct structure_name
{
	data_type member;
	data_type member;
	................;
	................;
	data_type member;
};

Example 1: create a structure of book to store, and display information.

#include<stdio.h>
//create a structure to store name and price of book.
struct Book
{
	char name[20];
	float price;
};

int main()
{
	struct Book b1;	//declare an object of structure
	printf("\nEnter Book Name : ");
	fgets(b1.name,20,stdin);	//string input
	printf("\nEnter Book Price : ");
	scanf("%f",&b1.price);
	printf("\nPrice of %s is %f",b1.name,b1.price);
return 0;
}
Sample Output:
Enter Book Name : The SECRET LETTERS
Enter Book Price : 310

Price of The SECRET LETTERS is 310.000

Members of structure are accessing using dot ( . ) operator in c language.

Array of Structure

We can also declare an array of structure in C to store multiple items. Below example is used to store and show information of product.

#include<stdio.h>
//create a structure to store name and price of Product.
struct Product
{
	char name[20];
	float price;
};
int main()
{
	struct Product p[3];	//declaring an array of structure
	int i;
	for(i=0;i<3;i++)
	{
		printf("\nEnter Product Name : ");
		scanf("%s",p[i].name);
		printf("\nEnter Product Price : ");
		scanf("%f",&p[i].price);
	}
	for(i=0;i<3;i++)
	{
		printf("\nPrice of %s is %f",p[i].name,p[i].price);
	}
return 0;
}
Sample Output:
Enter Product Name : Tea
Enter Product Price : 12
Enter Product Name : Coffee
Enter Product Price : 20
Enter Product Name : Snacks
Enter Product Price : 25

Price of Tea is 12.00
Price of Coffee is 20.00
Price of Snacks is 25.00

Pointer Structure in C.

We can also declare a pointer object of a structure, it is helpful in linked list creation. you can access an element of the structure by ( -> ) symbol, instead of dot operator.

#include<stdio.h>
//Example to create pointer structure.
struct Product
{
	char name[20];
	float price;
};
int main()
{
	struct Product *p;	//declaring pointer object of structure
	int i;
	printf("\nEnter Product Name : ");
	scanf("%s",p->name);
	printf("\nEnter Product Price : ");
	scanf("%f",&p->price);

	printf("\nPrice of %s is %f",p->name,p->price);

return 0;
}

Nested Structure

When we create an object of a structure in another structure is known as a nested structure. In this concept, one structure can contain another structure.

#include<stdio.h>
//Example nested structure.
struct date
{
	int day;
	int month;
	int year;
};
struct Employee
{
	char name[20];
	float salary;
	struct date doj; //creating object of another structure
};
int main()
{
	struct Employee e1;

	printf("\nEnter Name : ");
	scanf("%s",e1.name);
	printf("\nEnter Salary : ");
	scanf("%f",&e1.salary);
	printf("\nEnter Date of Joining(DD MM YY) : ");
	scanf("%d%d%d",&e1.doj.day,&e1.doj.month,&e1.doj.year);

	printf("\nEmployee Name : %s",e1.name);
	printf("\nSalary : %f",e1.salary);
	printf("\nDate of Joining : %d/%d/%d",e1.doj.day,e1.doj.month,e1.doj.year);

return 0;
}
Sample Output:
Enter Name : Rahul
Enter Salary : 24000
Enter Date of Joining(DD MM YY) : 01
06
2017

Employee Name : Rahul
Salary : 24000.00
Date of Joining : 1/6/2017