Home >>C++ Tutorial >C++ Data Structures

C++ Data Structures

C++ Data Structures

Variables that combine numerous data items of the same type are defined by the arrays in C or C++. Whereas, the data types that permits the programmer to combine data items of the different kinds is a kind of user defined data type that is known as data structure in C++.

In order to keeping or representing a record of something, structures are used in C++.
Let's take a case where you want to keep a record of the students in a school then there are several things that you might want to record like:

  • Studnet Name
  • Student Class
  • Student Roll number
  • Student Blood Group

Defining a Data Structure

Struct statement is generally used in order to define a structure in C++. The struct ststement generally defines a new data type that basically has more than one member that are for your program.

Here is the syntax of the struct statement in C++:

struct [structure tag] 
{
   member definition;
   member definition;
   ...
   member definition;
} 
[one or more structure variables];  

The definition of each member in structure tag is basically a normal variable definition like float f or any other valid variable definition. Please note that the structure tag is not mandatory, it is optional. Programmers can specify more than one structures variable at the end of the definition of the structure, just before the final semicolon. However, it is also considered optional.

Accessing Structure Members

Member access operator is used to access any member of a structure. Generally the member access operator is coded in the form of a period that lies between the structure variable name and the structure member that the programmer wants to access. The struct keyword is for the use of the programmer only when there is a need to define variables of the structure type. Here is an example for you to understand it better:

#include <iostream>
#include <cstring>
using namespace std;
struct students 
{
   char  name[50];
   char  qualification[50];
   char  blood_group[100];
};
 
int main() 
{
   struct students stu1;       
   struct students stu2;
   //Student 1 specification
   strcpy( stu1.name, "Shipra");
   strcpy( stu1.qualification, "MCA"); 
   strcpy( stu1.blood_group, "A+");
 
   //Student 2 specification
   strcpy( stu2.name, "Ravi");
   strcpy( stu2.qualification, "MCA"); 
   strcpy( stu2.blood_group, "A-");
 
   // Print Student 1 info
   cout << "Student 1 name : " << stu1.name <<endl;
   cout << "Student 1 class : " << stu1.qualification <<endl;
   cout << "Student 1 Blood group : " << stu1.blood_group <<endl;
   
   // Print Student 2 info
   cout << "Student 2 name : " << stu2.name <<endl;
   cout << "Student 2 class : " << stu2.qualification <<endl;
   cout << "Student 2 Blood group : " << stu2.blood_group <<endl;
    
   return 0;
}
Output :
Student 1 name : Shipra
Student 1 class : MCA
Student 1 Blood group : A+
Student 2 name : Ravi
Student 2 class : MCA
Student 2 Blood group : A-

Structures As Function Arguments

A structure can be passed as a function argument and the way is very similar to the way any other variable or pointer is passed in C++.

Here is an example for you to understand the concept from a better point of view:

#include <iostream>
#include <cstring>
using namespace std;
void printStudent( struct students stu );
struct students 
{
   char  name[50];
   char  qualification[50];
   char  blood_group[100];
};
 
int main() 
{
   struct students stu1;       
   struct students stu2;
   //Student 1 specification
   strcpy( stu1.name, "Shipra");
   strcpy( stu1.qualification, "MCA"); 
   strcpy( stu1.blood_group, "A+");
 
   //Student 2 specification
   strcpy( stu2.name, "Ravi");
   strcpy( stu2.qualification, "MCA"); 
   strcpy( stu2.blood_group, "A-");
 
   // Print Student 1 info
   cout << "Student 1 name : " << stu1.name <<endl;
   cout << "Student 1 class : " << stu1.qualification <<endl;
   cout << "Student 1 Blood group : " << stu1.blood_group <<endl;
   
   // Print Student 2 info
   cout << "Student 2 name : " << stu2.name <<endl;
   cout << "Student 2 class : " << stu2.qualification <<endl;
   cout << "Student 2 Blood group : " << stu2.blood_group <<endl;
    
	
	// Print student 1 info
   printStudent( stu1 );

   // Print student 2 info
   printStudent( stu2 );

   return 0;
}
void printStudent( struct students stu ) {
   cout << "Student Name : " << stu.name <<endl;
   cout << "Student Qulification : " << stu.qualification <<endl;
   cout << "Student Blood Group : " << stu.blood_group <<endl;
}
Output :
Student 1 name : Shipra
Student 1 class : MCA
Student 1 Blood group : A+
Student 2 name : Ravi
Student 2 class : MCA
Student 2 Blood group : A-
Student name : Shipra
Student class : MCA
Student Blood group : A+
Student name : Ravi
Student class : MCA
Student Blood group : A-

Pointers to Structures

Pointers can be defined to the structures in the very similar way as any programmer defines the pointer to some other variables.
Here is the syntax:

struct students *struct_pointer;

In the above mentioned pointer variable, the address of the structure variable can be stored.

The programmers have to place the & operator just before the structure's name in order to find the address of the structure variable.

struct_pointer = &stu1;

The following syntax should be used in order to access the members of a structure using a pointer to that very structure.

struct_pointer->name;

The typedef Keyword

There is an easier way to define structs or you could "alias" types you create.
For example −

typedef struct {
   char  name[50];
   char  qualification[50];
   char  blood_group[100];
} students;

Now, its possible to use students directly to define variables of students without using of struct keyword.
For example

Stu stu1 stu2