Structure passed as a reference – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

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

 

Source Code

  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|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back