copy structure in C++ | nested structure in c++

In this Page we will discuss about two following topics of structure,

copy structure in C++

As such we know that we can declare more than one variable of a structure. Similarly, we can copy values of any structure variables to any other variable, but remember provided both variables are of the same structure. Its program is given below-

In the program, we have declared two variables vr1 and vr2, we have also initialize even the variable vr1 as well ,such as

book vr1={11,"C++ tutorial",230.50}; 
book vr2;

Here we are copying the value of variable vr1 to the variable vr2,like

vr1= vr2;

Here is the program,

copy structure program in C++

#include<iostream>
using namespace std;

struct book {
    int sr;
    char name[20];
    float price;
  };

book vr1={ 
     11,
     "C++ Tutorial",
     230.50 
  }; // first structure variable initialization 

book vr2; //second structure variable declaration

int main()
 {
   // accessing values using first variable vr1
   cout<<"Serial No.: ";
   cout<<vr1.sr;
   cout<<"\nBook Name : ";
   cout<<vr1.name;
   cout<<"\nBook Price: ";
   cout<<vr1.price;

   cout<<"\nCopying values to other variable";

      vr2 = vr1; // copying value of vr1 to vr2

 // accessing values using first variable vr2
   cout<<"\nSerial No.: ";
   cout<<vr2.sr;
   cout<<"\nBook Name : ";
   cout<<vr2.name;
   cout<<"\nBook Price: ";
   cout<<vr2.price;
 
return 0;
 }

OUTPUT

Serial No.: 11
Book Name : C++ Tutorial
Book Price: 230.5

Copying values to other variable

Serial No.: 11
Book Name : C++ Tutorial
Book Price: 230.5

nested structure in C++

It is possible to make nested structure. nested structure similar to nested if else, nested loop or nested switch. In this one structure is a member of another structure. In this, to make one structure a member of another structure, make the variable of the earlier structure a member of another structure.

You can understand this with the syntax given below-

Firstly, declaring a structure

    struct first-structure-name { data-members; };

then declare first structure-variable inside to second structure using first structure-variable, such as-

struct {     //second structure 
   //      first structure variable declaration inside the second structure
      data -member ,
      structure-name variable-name; // first structure variable declaration
};

Here, data-member is the first structure member

Here you can see, in below,

struct first  // first structure
   { 
      int sr;
   };

 struct // second structure
   {
     first s_r;  // accessing first structure using their variable s_r;
     char name[20];
     float price;
   } vr;

accessing nested structure member

both structure members are accessed by the structure-variable of both. The outer-structure is used with a dot operator while the inner-structure is used twice with dot operator (first for outer-structure variable and second with inner-structure variable).

for inner structure member

second structure-variable.first-structure-variable.first structure-member name;

जबकि second structure के members साधारण तरीके से ही access होंगे।

second structure-variable.second structure-member;

 vr.name;
 vr.price;

Let’s try with an example

nested structure program in C++

In the program below, we have declared serial no (sr) in the first structure (struct first), whereas other details (name, price) are declared in another structure. when the program is executed, the serial no (sr) structure of the book will store in the first structure, whereas it will be accessed from the second structure variable vr.

#include<iostream>
#include<fstream>
#include<stdio>
using namespace std;

struct first{    // structure declare
  int sr;
}

struct {
   first s_r;  // assigning first structure into second structure using first structure variable s_r
   char name[20];
   float price;
} vr;

int main()
{
  cout<<“Enter Serial No.: “;
  cin>>vr.s_r.sr;         // read first structure 
  cout<<“Enter Book Name : “;
  gets(vr.name);
  cout<<“Enter Book Price: “;
  cin>>vr.price;

  cout<<“\nSerial No.: “<<vr.s_r.sr; // first structure member accessing
  cout<<“\nBook Name : “<<vr.name;
  cout<<“\nBook Price: “<<vr.price;
return 0;
}

OUTPUT

Enter Serial No.: 101
Enter Book Name : Programming
Enter Book Price: 350

Serial No.: 101
Book Name : Programming
Book Price: 350

Previous – Structure in C++
Next – union in C++


Like it?

Leave a Reply

Your email address will not be published.