in

CPP – Structures

Structure is a collection of variables of different data types under a single name. It is similar to a class in that, both hold a collection of data of different data types.

You can use structures to define your own data types; For example: Let’s say you want to store the details of students in a class like

  • Roll_Number
  • Name
  • Mobile_Number
  • Course
  • etc….

Using structures, we can reduce the redundancy and save our lot of memory.

How to Define Structures?

Every structure starts with a struct keyword following the name of the structure(user-defined datatype). Structures are enclosed in curly braces with a ; after the last brace. It is not compulsory to declare structure variable in structure itself; you can declare it anywhere outside of the structure.

struct [Name] {
data_member;
data_member;

data_member;
} [Structure_Variable];

How to access data members?

Data members of a structure can be accessed by using “.” (member access operator).

Structure_Variable.data_member;

Note that we are not using struct keyword again while accessing the data members as we do in C language. That is the new feature of C++.

#include <iostream>
#include <string>
using namespace std;

struct Students{
  string name;
  double roll_no;
  string course;
  unsigned int mobile_no;
};

int main()
{
  Students st1; //Declaring the variable of the structure Student
  //Accessing the data_members
  st1.name = "Anandesh";
  st1.roll_no = 8;
  st1.course = "BCA";
  st1.mobile_no = 999694494;
  cout << "Name of the student: " << st1.name << endl;
  cout << "Roll Number of the student: " << st1.roll_no << endl;
  cout << "Course of the student: " << st1.course << endl;
  cout << "Mobile Number of the student: " << st1.mobile_no << endl;
  return 0;
}

Name of the student: Anandesh
Roll Number of the student: 8
Course of the student: BCA
Mobile Number of the student: 999694494

Passing Structure as Function Arguments

You can pass a structure to a function, rest all things will be same as in structure defining and declaration.

Beware!! Dude…..while passing the structure for manipulation or initializing the data_members value in other function. If you don’t use a reference or a pointer it will not manipulate values in actual data_members.

#include <iostream>
using namespace std;

struct Students{
  string name;
  double roll_no;
  string course;
  unsigned int mobile_no;
};
//Passing the reference of the structure Students variable so that manipulation can be done in actual location
void insertData(Students& st1)
{
  st1.name = "Anandesh";
  st1.roll_no = 8;
  st1.course = "BCA";
  st1.mobile_no = 999694494;
}
//Passing the reference and showing the output
void displayData(Students& st1)
{
  cout << "Name of the student: " << st1.name << endl;
  cout << "Roll Number of the student: " << st1.roll_no << endl;
  cout << "Course of the student: " << st1.course << endl;
  cout << "Mobile Number of the student: " << st1.mobile_no << endl;
}
int main()
{
  Students st1;
  insertData(st1);
  displayData(st1);
  return 0;
}

Name of the student: Anandesh
Roll Number of the student: 8
Course of the student: BCA
Mobile Number of the student: 999694494

Pointers to Structures

You can define pointers to structures in very similar way as you define a pointer to any other variable as follows:

struct Students *struct_pointer;

Now, you can store the address of a structure variable in the above-defined pointer variable. To find the address of a structure variable, place the & operator before the structure’s name as follows:

struct_pointer = &st1;

To access the members of a structure using a pointer to that structure, you must use the -> operator as follows

struct_pointer->name;
#include <iostream>
using namespace std;

struct Students{
  string name;
  double roll_no;
  string course;
  unsigned int mobile_no;
};
int main()
{	
  Students *ptr = new Students; //allocating new space for Students so that ptr can points to that allocation of memory
  //You can also create a variable first-------------####  Students st1;
  //Then point the ptr to that structure variable----####  ptr = &st1;
  ptr->name = "Anandesh";
  ptr->mobile_no = 999694494;
  ptr->roll_no = 8;
  ptr->course = "BCA";

  cout << "Name of the student: " << ptr->name << endl;
  cout << "Roll Number of the student: " << ptr->roll_no << endl;
  cout << "Course of the student: " << ptr->course << endl;
  cout << "Mobile Number of the student: " << ptr->mobile_no << endl;
  return 0;
}

Name of the student: Anandesh
Roll Number of the student: 8
Course of the student: BCA
Mobile Number of the student: 999694494

Written by Anandesh

I am a #Linux lover and very much excited about technology and new thing. Love to #read_books. Curiosity is my best teacher. I am still☺️ understanding myself.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CPP – References

CPP – Classes & Objects