Structure passed as a reference – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#2979 siteicon   siteicon   siteicon  

Structure passed as a reference

A program to demonstrate how an structure is passed as a reference argument to a function.

Learning Objectives

  • Passing structure to a function as a reference argument. Also understanding the difference if it is passed by value.

Source Code

TC++ #2979

#include <iostream>
#include <string.h>
#include <conio.h>
struct Person
{
char name[40];
int age;
};

void modify(Person &p,char n[],int a=16)
{
strcpy(p.name,n);
p.age=a;
}

int main()
{
clrscr();
Person pers={"Lata",16};
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Arun",17);
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Suraj");
cout<<pers.name<<", "<<pers.age<<endl;
getch();
return 0;
}

Source Code

#include <iostream>
#include <string.h>
using namespace std;
struct Person
{
char name[40];
int age;
};

void modify(Person &p,char n[],int a=16)
{
strcpy(p.name,n);
p.age=a;
}

int main()
{
Person pers={"Lata",16};
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Arun",17);
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Suraj");
cout<<pers.name<<", "<<pers.age<<endl;
return 0;
}

Test it !
  Related (?) :

Run Output

Lata, 16
Arun, 17
Suraj, 16

Code Understanding

struct Person { char name[40]; int age;};
A structure Person defined with char array name and age variables. Instance of this will be modified by program by reference passing.

void modify(Person &p,char n[],int a=16)
Here the structure instance p has been passed as &p means it is passed as reference. This means that original copy of structure instance would be affected. Here the char array is also passed by reference and variable a is passed by value with a default value of 16.
{
strcpy(p.name,n); p.age=a;
Original structure instance which is passed would be affected with this instruction. For name being a char array strcpy function is used and for age the value has been directly updated with an assignment.
}

 

Person pers={“Lata”,16};
Structure instance pers initialised with name and age.

cout<<pers.name<<“, “<<pers.age<<endl;
This prints as per initialisation

modify(pers,”Arun”,17);
This will modify the structure instance pers value as it has been passed with reference.

cout<<pers.name<<“, “<<pers.age<<endl;
Modified values shall be printed.

modify(pers,”Suraj”);
This will again modify the values of pers. Here name has been passed but age has not been passed. In this case default value of age given with function definition will come into picture.

cout<<pers.name<<“, “<<pers.age<<endl;
Modified values shall be printed again.

Notes

  • Since structures instances can be of very large size, thus passing them by value may not be recommended as a very large memory copying operation is required for that. SO! passing by reference as done in this example is a good technique. Although programmer needs to take additional care that the original data if required later in calculations should be saved in some other variables.

Common Errors

  • Student may accidentally forget to put the & while passing the structure by reference. It is advised that they should try it without giving this sign and see the difference. If you remove & sign in the function header then the output would be –
    Lata, 16
    Lata, 16
    Lata, 16Please do try with this error and analyse.


Suggested Filename(s): struc2fnr.cpp



Share

sunmitra| Created: 24-Jan-2018 | Updated: 24-Jan-2018|






Back