Table of contents
1.
Introduction
2.
Creating a Struct
3.
Declaring Struct variables
3.1.
Declaring structure variables during structure declaration
3.1.1.
Example
3.2.
Declaring structure variables like basic data types
3.2.1.
Example
4.
Initializing Struct members
4.1.
Example
5.
Accessing struct elements
5.1.
Example
5.2.
Output
6.
Sorting Structs
7.
Example
7.1.
Structure Declaration
7.2.
Program to sort struct class
7.3.
Output
8.
FAQs:
9.
Key Takeaways
Last Updated: Mar 27, 2024

Structure sorting in C

Author Ayushi Poddar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Struct which is short for structure is user-defined datatypes found in C and C++. It is different from arrays as the same holds data items of the same datatype whereas struct can hold data items of different data types under a single name.

Also See, Sum of Digits in C and C Static Function.

Creating a Struct

The keyword ‘struct’ is used to create a structure in C and C++. Below is the syntax for declaring a struct.

struct struct_name
{
//Members declaration
//Datatype declaration
type_1 member1; 
type_2 member2;
.
.
.
type_n membern;
};

Declaring Struct variables


There are two ways to declare struct variables. Either it can be declared during struct declaration or declared as basic datatypes. Let us study more about it.

Declaring structure variables during structure declaration


We can add optional object names before we place the semicolon at the end of the structure definition. These object names are used to initialize struct objects of a similar kind.

Example

struct Adult
{
char full_name;
int age;
}Professional,Student;

Declaring structure variables like basic data types


To declare struct variables like basic data types, the struct_name is used to create new objects.
 

Example

struct Adult
{
string full_name;
int age;
};
int main()
{
Person Professional;
Person Student;
}

Initializing Struct members

This is done using parenthesis ‘{}’ and struct members are initialized by the means of struct objects and is not possible through declaration. 

Example

struct Adult
{
string full_name;
int age;
};

int main()
{
Adult Professional = {"Sita",35 };
Adult Student = {"Gita",23 };
}

Accessing struct elements

The struct elements can be accessed by dot (.). It can be explained through the following example. 

Example

#include <bits/stdc++.h>
using namespace std;


struct Adult
{
string full_name;
int age;
}Professional,Student;


int main()
{
Professional.name = "Sita";
Professional.age = 35;

Student.name = "Gita";
Student.age = 23;

// we access the elements here using the "." (dot) and display
cout <<Professional.name <<" "<<Professional.age << endl ;
cout << Student.name << " "<< Student.age;
}

Output

Sita 35
Gita 23

You can also read about the dynamic arrays in c, and  Tribonacci Series

Sorting Structs

In the Sorting of structures, all the properties of the structure object are sorted on the basis of the properties of the object. To sort the given data which might be in form of an array of integers or strings in ascending order, let's take an example.

Example

Suppose you have been given a class database and you are assigned to sort names of students according to their enrolment number. So for that, we first need to create a structure that can store their roll numbers and names. Then a compare function is created to sort the data. In the main function, an array is created to take the input for the roll number and name of the students. Then we call the sort function to process the input. And then the sorted list is displayed using a for loop. 

Structure Declaration

typedef struct class{
    int roll_no;
    string sname;
    }data;

 

Program to sort struct class

#include <bits/stdc++.h>
using namespace std;
//struct declaration
typedef struct class{
int roll_no;
string sname;
}data;
//function to sort
bool compare(data a, data b)
{
//for descending order replace with a.roll_no >b.roll_no
if(a.roll_no < b.roll_no) 
return 1;
else
return 0;
}
//main function
int main()
{
int n,i;
cout<<"Enter the Total number of students : \n";
cin>>n;
//array of structure creation
data array[n];
cout<<"Enter roll number and student name :\n";
for(i=0;i<n;i++)
{
cin>>array[i].roll_no;
cin>>array[i].sname;
}
sort(array,array+n,compare); //sort function call
cout<<"Sorted list : "<<endl;
for(i=0;i<n;i++)
{
cout<<array[i].roll_no<<" ";
cout<<array[i].sname<<endl;
}


return 0;
}

Output

Enter the Total number of students :
2
Enter roll number and student name :
001 Ayushi
002 Abhishek
Sorted list : 
001 Ayushi
002 Abhishek

Time Complexity: O(n), since we have a for loop running for n number of times.

Space Complexity: O(n), since an array has been created for n number of students.

Also see,  Short int in C Programming

FAQs:

  1. What is struct?
    The keyword ‘struct’ is used to create a structure in C and C++. It is different from arrays as the same holds data items of the same datatype whereas struct can hold data items of different data types under a single name.
  2. How can struct be declared?
    There are two ways to declare struct variables. Either it can be declared during struct declaration or declared as basic data types.
  3. How is struct accessed? 
    The struct elements can be accessed by dot (.) for example if the object is student and the member is name in the struct then it can be called by student.name.

Key Takeaways

In this article, we have extensively discussed the following things:

  1. What is struct?
  2. How to declare struct variables and access it?
  3. How to sort a struct.

Recommended Problems - 


You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

 

 

Live masterclass