C++

Pass String by Reference in C++

The C++ reference is a name for a variable that already exists. A reference to a variable can’t be altered to refer to the other variable once initialized. Pointers or references can be passed as parameters to functions in C++. Thus, the outcome is identical in both circumstances. The passing by reference enables a function to update a variable without creating a copy. We must declare reference variables so that the parameter and variable are passed to share the same memory location. Any changes that occur in the parameter also affect the variable.

With the help of examples, we will understand C++ passing by reference approach and how to provide pointers as arguments to functions in this article. The parameters passed to function as pointers are the same as any other value when passing a value by reference. As a result, you must provide pointer types for the function arguments, like in the swap() function, which swaps the values of various integer variables addressed by their arguments.

Example 1: Program of Passing by Reference Without Pointers in C++

Here, we pass a value by reference without utilizing the pointer declaration. Below is the illustration program for passing a variable by reference.

Initially, we have included the header files for the program implementation in the header section. In the next step, we have a function definition for swapping values. The function is given a name swap, and the function takes two string variable references as a parameter. The string variables the swap function will apply are defined as “str1” and “str2” reference strings.

Then, in the function, we created a variable “temp,” which we passed the variable “str1”. After that, the “str2” is assigned to “str1,” and then “str2” has the “str2”. In this way, the swap algorithm is applied to the reference string.

We have our main function where two strings are declared as “str_A” and “str_B” and initialized with some string values. The string values will be printed before the swap function is applied. Then, we have invoked the swap function in the main function and passed the string defined in the main function. after that, the swapped string will be printed.

#include <iostream>

using namespace std;

void swap(string &str1, string &str2) {
    string temp;
    temp = str1;
    str1 = str2;
    str2 = temp;
}
int main()
{
    string str_A = "c++", str_B = "programming";

cout<< "Strings Before swapping" <<endl;
cout<< "string 1 : " <<str_A<<endl;
cout<< "String 2 : " <<str_B<<endl;
swap(str_A, str_B);

cout<< "\nStrings After swapping" <<endl;
cout<< "string 1 : " <<str_A<<endl;
cout<< "string 2 : " <<str_B<<endl;

    return 0;
}

The outcome shows the string before swapping and after swapping through the reference string passed in the function.

Example 2: Program of Passing by Reference With Pointers in C++

As in the preceding example, we have only seen the passing string by reference. Therefore, we’ll use pointers by reference in C++ in this example.

The program begins by creating a function that is represented with the name “SwapString” and passes two-pointer strings as an argument. Then, we have called the program’s main function. In the main function, the two strings are named “str1” and “str2,” respectively. These string variables are initialized with the string of words.

Then, we have called the function “SwapString,” to which string variables “str1” and “str2” addresses are passed. The strings will be swapped in this function definition in the main function and printed. After that, we have called the function “SwapString” outside the main function for swapping the specified strings.

#include <iostream>

using namespace std;

void SwapString(string*, string*);

int main()
{
    string str1 = "hello", str2 = "friends";

cout<< "Strings Before swapping" <<endl;
cout<< "Str1 = " << str1 <<endl;
cout<< "Str2 = " << str2<<endl;
SwapString(&str1, &str2);

cout<< "\nStrings After swapping" <<endl;
cout<< "str1 = " << str1 <<endl;
cout<< "str2 = " << str2 <<endl;
    return 0;
}
void SwapString(string* s1, string* s2) {
    string temp;
    temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

Hence, the results of passing string references with pointers are shown in the following graphic.

Example 3: Program of Passing String Object by Reference in C++

Even if a function doesn’t get to modify the source string object in the calling program, passing C++ string objects via reference is fairly common. Objects are usually quite large, and thus it can be costly compared to the amount of storage they use and the time necessary to construct a clone of them when passing them by value. So generally, passing objects by reference saves both memory and time.

The sole disadvantage of passing an object via reference is that it can alter the source object that was passed to the function. That is not desired. If we don’t want to update an object in the function, we would prefer to make it difficult.

We have a function definition in the following program as “InputString,” to which we passed string reference. Then, we have declared a string object “MyStr” in the main function and the object string “MyStr” contains a string of a word.

After that, we called an “InputString” and passed this string object into it. We have a function definition “InputString” outside the main function, which creates a new string object reference from the “MyStr”. The new string is defined as “NewStr” and then initialized in the function’s body. We have modified the object string “NewStr” and printed the new string object.

#include <iostream>

#include <string>

using std::cout;
using std::endl;
using std::string;

void InputString(string&);

int main()
{
    string MyStr = "Wel";
cout<< "String value: " <<MyStr<<endl;
InputString(MyStr);
cout<< "String value now :" <<MyStr<<endl;

    return 0;
}

void InputString(string&NewStr)
{
cout<< "String value in function :" <<NewStr<<endl;
NewStr = NewStr + "come";
cout<< "String value now in function :" <<NewStr<<endl;
}

The graphic below represents the resultant string values of the source string and changed string.

Example 4: Program of Passing a Constant String Object by Reference in C++

The compiler will throw a fault if a reference is passed to the constant object. We can tackle this problem by using a constant variable reference. This prevents the variable to which the reference points from being changed.

First, we have the function definition “DisplayString,” where a constant string reference is passed. The constant strings are defined and initialized in the main function as “str1” and “str2”. After that, pass these constant strings to the function “InputString”. Invoked the function outside the main function where we have declared a constant string variable “Mystr”.

#include <iostream>

#include <string>

using std::cout;
using std::endl;
using std::string;
void DisplayString(const string&);
int main()
{
    const string str1 = "Infinix";
    string str2 = "Iphone";
cout<< "str1 :" << str1 <<endl;
DisplayString(str1);
cout<< "str2 : " << str2 <<endl;
DisplayString(str2);
    return 0;
}
void DisplayString(const string&MyStr)
{
cout<< "MyStr : " <<MyStr<<endl;
}

A non-constant object is passed to a function through a constant object reference. So we don’t get any compilation errors in the code.

Conclusion

References enable a function to affect the value of an argument, which can be beneficial in some situations. Const references, in contrast, ensure that the function does not change the argument. Thus, we have concluded the article here with the example demonstration in C++ and hope it might be helpful.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content