Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can you explain the code please? (C++)

+1 vote
asked Jun 8, 2023 by Eidnoxon (4,900 points)

I just watched a tutorial on how to swap 2 variables, and the guy showed this code:

#include <iostream>

using namespace std;

void swap(string &x, string &y);

int main() {

string x = "Kool-aid";

string y = "Water";

swap(x, y);

cout << x << endl;

cout << y << endl;

return 0;

}

void swap(string &x, string &y){

string temp = x;

x = y;

y = temp;

}

I know what this code does. I only have one part I don't understand. Why do I need `&` here:
void swap(string &x, string &y);

I know that `&` shows the memory where the file is located in, but why do we need that?

Please tell me the answer, and if you can, tell me how would you write the same code! :D

I would appreciate your answer :D

1 Answer

0 votes
answered Jun 8, 2023 by Peter Minarik (82,920 points)

I know that `&` shows the memory where the file is located in, but why do we need that?

Not quite so.

The & operator returns the address of a variable. E.g. the following little program gets the pointer (memory address) of an integer and uses that pointer to change the value of the pointed variable.

#include <iostream>

int main()
{
    int * pointerToMyAge = &myAge;
    *pointerToMyAge = 22;
    std::cout << "My age changed via a pointer: " << myAge << std::endl;

    return 0;
}

However, this is not the case in your code. In your code the & operator has a different meaning, it means reference.

References are similar to pointers, but unlike pointers, they do not need to be dereferenced (e.g.: *pointerToMyAge) before you can read/write their value (and not where they point to).

So in the context void swap(string &x, string &y); the & operator tells the compiler that both x and y are not mere strings but references to strings.

We have to take a side step here... When you pass arguments to a function, you can do it in two ways: either as a value or as a reference. If you pass arguments as values, a copy is made. If you pass them as a reference, the original variable and the parameter of the function will not only have the same value, but they also reference the same memory address: changing one would change the other.

So by passing x and y as string references swap() can replace their values. You can easily check this by removing the & operator. The code will compile. It will run but at the end "Kool-aid\nWater" will be printed and not the other way around. However, inside swap() their values got replaced. But since this time we did not use references, just copies (value-based parameter passing) nothing "leaves" the function swap().

I hope this makes sense.

Good luck!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...