copy structure

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

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,

#include<iostream.h>
#include<conio.h>

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

void main()
{
  clrscr();
 // 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;
 
getch();
}
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

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 syntex given below-

Firstly, declaring a structure

struct inner structure-name {data-member; };

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

struct { //outer structure 
  structure-name variable; // first structure variable declare inside the second structure
  structure_name variable_name; // inner structure
};

Here, data-member is the  first structure member

Here you can see, in below-

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

struct // outer structure
  {
    first s_r;  // accessing inner 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

outer structure-variable.inner-structure-variable.inner structure-member;

vr.s_r.sr;        

for outer structure member (just in normal structure), 

outer structure-variable.outer structure-member;

vr.name;
vr.price;

Let’s try with an example

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.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>

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;

void main()
{
  clrscr();
  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;
  getch();
}
OUTPUT:-
Enter Serial No.: 101
Enter Book Name : Programing
Enter Book Price: 350

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

 

previous-Structure in C++

next-union in C++