Structures in C++ Language

Structures in C++ Language

Introduction to structure

A structure is a very user friendly data type which lets the user define the types of different data which can be stored in the structure, i.e we can store integer, float, character, and string in one place(under one identifier). The elements saved in a structure are called it’s members. It is widely used to store student information like name, age, marks and other such data in one structure. Structure is the same as an array. The difference is that, In array we can store same type of data but in structure  we can store different type of data.

Structure in C++ Language

Once you create a structure, then it becomes a data type. Now you can create any variables of this data type. And you can also create an array of this data type. But when you initialize the value of this variable, you have to initialize the value of all the variables defined in that structure. We can define the structure before the main method.

Defining the Structure

Struct keyword is used to define the structure. After this keyword the structure is given the unique name. After this, variables are created in curly braces and semicolon is applied after the ending curly bracket.

struct struct_Name
{
   Structure member 1;
   Structure member 2;
    ...
  Structure member N;
};

Let’s take an example :

struct Student
{
   int stu_id;
   char stu_name[10];
   int stu_age;
   char stu_branch;
};

Here, the name of the structure is taken in the name “Student” and there are four members of the structure,which have two data types. An integer for a ‘stu_id’ and ‘stu_age’ and character for a ‘stu_name’ and ‘stu_branch’.

Structures in C++ Language diagram

Structure Variable Declaration

Structure variables can be declared in two ways:-

  1. Structure variable is written even when the definition of structure is written.
  2. The variable of the structure is also done inside the main() function.

Syntax  for  Structure  variable outside of structure definition

struct structure_name
{
   data_type member 1;
   data_type member 2;
      ......
   data_type member n;
}structure_variable(s);

Example:-

struct Student
{
   int stu_id;
   int stu_age;
   char stu_name[10];
   char stu_branch[10];
}info;

Syntax for structure variable in main() function.

struct structure_name
{
   data_type member 1;
   data_type member 2;
            ......
   data_type member n;
}structure_variable(s);
int main()
{
struct structure_name structure_variable_name;
}

Example:-

struct Student
{
   int stu_id;
   int stu_age;
   char stu_name[10];
   char stu_branch[10];
};
int main()
{
struct Student info;
}

Accessing Structure Members

It can be assigned to values in many ways. Without the structure the members of the structure have no personal meaning. To provide a value to a member of any structure, the member name must be connected with the structure variables using dot(.). The operator is also called the term or member access operator.

Syntax for Accessing Structure members:-

structure_variable_name.member_of _structure =value(optional);

Example:-

Run

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

struct Student
{
  char stu_name[10];
  int stu_age;
  char stu_branch;
  int stu_id;
};
int main ()
{
  struct Student s1;		// s1 is a variable of Student type and
  s1.stu_age = 17;		//age is a member of student

  // using string function to add name
  strcpy (s1.stu_name, "Soumya");

  cout << "Name of student 1:" << s1.stu_name << endl;
  cout << "Age of student 1:" << s1.stu_age << endl;

  return 0;
}

Output:-

Name of student 1:Soumya
Age of student 1:17

Structure Initialization

The  structure initialization is of two type.
We can also initialize the structure variable at compile time.

TYPE 1:-

struct Student
{
  float height;
  int weight;
  int age;
};
struct Student s1 = { 180.75 , 73, 23 };        //initialization 

TYPE 2:-

struct Student s1;
s1.height = 182.5 ;         //initialization of each member separately
s1.weight = 65;
s1.age = 20;

Structure working with size(sizeof)

The member of the structure allocates different memory and the size of the complete structure is as its members.

Example:-

Run

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

struct Student
{
int stu_id;
char stu_name[10];
};

int main()
{
struct Student info;

cout<< "size of Student id is: "<< sizeof(info.stu_id) << " bytes" << endl;  //size of stu_id
cout<< "size of Student name: " << sizeof(info.stu_name) << " bytes" << endl; //size of stu_name
cout<< "size of Student structure: "<< sizeof(info) << " bytes" << endl;       //size of Student
return 0;
}

Output:-

size of Student id is: 4 bytes
size of Student name: 10 bytes
size of Student structure: 16 bytes

Array of Structure

We can create an array of structure like other primitive data types. In which each element of array will represent a structure variable.

Example :

 struct student stu[5];

The below program defines an array stu of size 5.

Run

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

struct Student
{
  char name[10];
  int id;
};
struct Student stu[5];
int i, j;
void ask ()
{
  for (i = 0; i < 3; i++)
    {
      cout << "enter student record: " << i + 1;
      cout << "Student name: "; cin >> stu[i].name;
      cout << endl;
      cout << "enter id: "; cin >> stu[i].id;
    }
  cout << "displaying Student record:" << endl;;
  for (i = 0; i < 3; i++)
    {
      cout << "Student name is " << stu[i].name << endl;
      cout << "id is " << stu[i].id << endl;
    }
}

int main ()
{
  ask ();
  return 0;
}

Output:

enter student record: 1Student name: Rahul
enter id: 567
enter student record: 2Student name: Srishty
enter id: 476
enter student record: 3Student name: Tanya
enter id: 589
displaying Student record:
Student name is Rahul
id is 567
Student name is Srishty
id is 476
Student name is Tanya
id is 589

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription