Structures in C with Examples

The C programming language has many awesome features which help the programming by making their coding easy. In C, you can also make use of structures. It will help you in creating data items of different types. Structure is one of the important and efficient topics in C. A programmer should learn the functionality of structures because of its working and easy-to-implement methods.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

What is Structure in C?

Structure is a user-defined data type. It works similarly like arrays. Structures help you in grouping items of different types in a single group. It stores the collection of different data types. Each element of structure is known as a member.

Defining structures in C

Before creating variables of structure, you have to define it first. You can use the struct keyword to define structures.

Syntax:-

struct name_of_the_structure 
{
    data_type member1;
    data_type member2;
    ...
    data_type memberN;
};

Example of C Structures

struct bill
{
   float amount;
   int id;
   char address[100];
};

In the above example, we have defined a structure named bill. And the members of this structure are amount, id and address.

How to create structures in C:-

You can create structures outside the main() function. The struct keyword is used to create a structure in C. To access the data members of a structure, you will have to create structure variables either outside or inside of the main() function.

Syntax:-

struct name_of_the_structure 
{
    data_type member1;
    data_type member2;
    ...
    data_type memberN;
};

Example:-

struct bill
{
   float amount;
   int id;
   char address[100];
};

Creating structure variable in C

You can also declare variables through the structure. It helps in accessing the members of the structure easily. There are two ways to declare structure variable:-

  • Using the struct keyword within the main() function.
  • You can also declare a variable at the time of defining a structure.

First Method of Creating structure variable in C

struct bill
{
   float amount;
   int id;
   char address[100];
};

In first method, you can declare the structure variables within the main() function like below:-

int main(){
struct bill p1,p2;
}

From the above example, you can use p1,p2 variables to access the members of the structure.

Second Method of Creating structure variable in C

You can also declare a structure variable in other way like below:-

struct bill
{
   float amount;
   int id;
   char address[100];
}p1,p2;

Between these two ways which way is better:-

  • If the number of variables which you want to declare are fixed then use the second method of declaring structure variables.
  • If the number of variables is not fixed then use the first way of declaring structure variables.

Accessing members of C structures

Before using the structure variables, you will have to access the members of the structure. There are 2 ways to access the member of the structure:-

  • Use . operator
  • Use -> operator (structure pointer operator)

Suppose, you want to access the amount member of the p1 variable from the above example then you can use . operator like below:-

p1.amount;

Example:- Accessing members of structures

#include<stdio.h>  
#include <string.h>    
struct bill 	 
{   int id; 	 
  char address[200]; 	 
  float amount; 	 
}p1,p2;	 
int main( )    
{   
printf("TechVidvan Tutorial: Accessing members of structure!\n\n");
   p1.id=1;    
   strcpy(p1.address, "Sector 41B, Market Complex, City: Siliguri, State: West Bengal");  
   p1.amount=5689.36;
   printf("Details of First Person!\n");
   printf("Id of first person is: %d\n",p1.id);
   printf("Amount due by first person is: %f\n",p1.amount);
   printf("Address of first person is: %s\n",p1.address);
   p2.id=2;    
   strcpy(p2.address, "Sector 43B, Road No-06, Market Complex, City: Siliguri, State: West Bengal");  
   p2.amount=5644.36;
   printf("Details of Second Person!\n");
   printf("Id of Second person is: %d\n",p2.id);
   printf("Amount due by second person is: %f\n",p2.amount);
   printf("Address of second person is: %s\n",p2.address);    
    
   return 0;    
}  

Output:-

TechVidvan Tutorial: Accessing members of structure!

Details of First Person!
Id of first person is: 1
Amount due by first person is: 5689.359863
Address of first person is: Sector 41B, Market Complex, City: Siliguri, State: West Bengal
Details of Second Person!
Id of Second person is: 2
Amount due by second person is: 5644.359863
Address of second person is: Sector 43B, Road No-06, Market Complex, City: Siliguri, State: West Bengal

Array of structures in C

You can also create an array of structures like below:-

#include<stdio.h>
struct array_name
{
   int a;
   float b;
};
int main()
{
   struct array_name a1[6];
   a1[0].a = 44;
   a1[1].b = 4.5;
   printf("TechVidvan Tutorial: Array of structures!\n\n");
printf("Integer value is: %d\n",a1[0].a);
   printf("Floating point value is: %0.1f",a1[1].b);
   return 0;
}

Output:-

TechVidvan Tutorial: Array of structures!

Integer value is: 44
Floating point value is: 4.5

Structure Pointer in C

As you have seen, you can use an array to structure. Similarly, you can use a pointer to the structure. If you use pointers to a structure then you will have to use -> operator to access the members of the structure.

Example of structure pointer

#include<stdio.h>
struct point
{
   int a;
   float b;
};
int main()
{
   struct point p1={3,6.3};
   struct point *p2=&p1;
   printf("TechVidvan Tutorial: Pointer to structures!\n\n");
   printf("First value is: %d\n",p2->a);
   printf("Second value is: %0.1f",p2->b);
   return 0;
}

Output:-

TechVidvan Tutorial: Pointer to structures!

First value is: 3
Second value is: 6.3

typedef keyword in C

You can use the typedef keyword with structures to simplify the syntax for declaring variables. With the help of this keyword, you can give a meaningful name to an existing variable in your C program.

Let’s look at an example below:-

struct bill
{
   float amount;
   int id;
   char address[100];
};
int main(){
struct bill p1,p2;
}

In a similar way, you can use the typedef keyword in the above example like below:-

typedef struct bill
{
   float amount;
   int id;
   char address[100];
}each_bill;
int main(){
each_bill p1,p2;
}

Nested structures in C:-

Like nested loops, you can also create nested structures in your program code.

Example of C nested structures

struct detail
{
 int id;
 float amount;
};

struct info
{
   struct detail each_person;
   int age;
} person_1, person_2;

In the above example, we have used two struct types: detail and each_person. Suppose you want to declare a value of the member id for the variable person_1 then you can do this like below:-

person_1.each_person.id = 4;

Passing function to structures in C

You can also pass a function to structures. It helps you in making your coding better and efficient.

Example of passing function to structures

#include <stdio.h>
#include <string.h>
 
struct info {
   char student_name[50];
   char favorite_subject[20];
   int id;
};
void display( struct info each_student );
int main() {
   struct info s1;  	 
   strcpy( s1.student_name, "John Digoi");
   strcpy( s1.favorite_subject, "Maths");
   s1.id = 756;
// printing first student details!
   display(s1);
   return 0;
}

void display( struct info each_student ) {
  printf("TechVidvan Tutorial: passing a function to the structures!\n\n");
   printf( "Name of first student: %s\n", each_student.student_name);
   printf( "Favourite subject of first student: %s\n", each_student.favorite_subject);
   printf("ID of first student: %d\n",each_student.id);
}

Output:-

TechVidvan Tutorial: passing a function to the structures!

Name of first student: John Digoi
Favourite subject of first student: Maths
ID of first student: 756

Why to use structures in C?

Sometimes you will face a situation where you want to store various information or data about one or more people. That is when you will create different variables for each information per person. And, your program code will be long and complex. So to avoid this type of long and complex coding, you can create a collection of all related information with the help of structures.

Limitations of structures in C

  • You cannot use operators like +,- on struct variables.
  • You cannot hide any data through structures.
  • It does not allow functions inside structures.
  • Structures do not contain static data members.
  • You cannot use any access specifiers on structure because the C programming language does not support access modifiers.
  • You cannot use constructors inside the structures.

Significance of C Structures

Let’s take an example to understand the significance of structures in C.

Let’s say that you want to display a particular date in your system. With the help of a structure, you can define the date, month and the year as the members of the structure. It will work as a single unit.

Following is the solution to the above problem:-

#include <stdio.h>
struct printDate
{
int d, m, y;
};
int main()
{
struct printDate date = {16, 11, 2021};
printf("TechVidvan Tutorials: Significance of structures in C!\n\n");
printf("Your provided date is: %d:%d:%d\n", date.d, date.m, date.y);
return 0;
}

Output:-

TechVidvan Tutorials: Significance of structures in C!

Your provided date is: 16:11:2021

Designated Initialization in C

The main purpose of designated initialization is to initialize the structure members in any order.

Example of C Designated Initialization

#include<stdio.h> 
struct init
{
   int a, b, c;
}; 
int main()
{
   // using designated initialization
   struct init i1 = {.b = 1, .a = 4, .c = 47};
   struct init i2 = {.b = 13};
   printf("TechVidvan Tutorial: Designated initialization!\n\n");
   printf ("Values: a = %d, b = %d, c = %d\n", i1.a,i1.b,i1.c);
   printf ("Values: b = %d", i2.b);
   return 0;
}

Output:-

TechVidvan Tutorial: Designated initialization!

Values: a = 4, b = 1, c = 47
Values: b = 13

Summary

The C programming language has many awesome features which help the programming by making their coding easy. In C, you can also make use of structures.

Structure is a user-defined data type. It works similarly like arrays. You can create a collection of all related information with the help of structures. Before using the structure variables, you will have to access the members of the structure.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.