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:
-
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.
-
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.
-
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:
- What is struct?
- How to declare struct variables and access it?
- How to sort a struct.
Recommended Problems -
You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.