-2

Look at the following code:

#include <iostream>
#include <string>
using namespace std;
void reverseStr(string arr, int start, int end)
{
    while (start < end)
    {
        swap(arr[start], arr[end]);
        ++start;
        --end;
    }
}
int main()
{
    string str = "CPP";
    reverseStr(str, 0, 3);
    cout << str;
    return 0;
}

The output is CPP, while PCC is expected.

Question: How to pass string by reference in C++? Are strings a normal array? If yes then the reference must be passed automatically, while it just creates a copy in the formal parameters.

My question is that why I have to use my function like void reverseStr(string &arr, int start, int end) instead of just void reverseStr(string arr, int start, int end). Why I have to use extra & informal parameters? Aren't string variables just other arrays?

walnut
  • 21,629
  • 4
  • 23
  • 59
Pikachu
  • 304
  • 3
  • 14
  • 2
    `void reverseStr(string&, int, int)` is string just a normal array? _no_, it is not. – Chad Feb 15 '20 at 14:03
  • My question is that why I have to use my function like `void reverseStr(string &arr, int start, int end)` instead of just `void reverseStr(string arr, int start, int end)`. Why I have to use extra `&` informal parameters? Aren't string variables just other arrays? – Pikachu Feb 15 '20 at 14:04
  • 3
    No, strings are not arrays. Unless you see `[]` somewhere in the vicinity of a ***declaration***, it's not an array. – Sam Varshavchik Feb 15 '20 at 14:06
  • 1
    By the way, although they're still taught in some Asian academic curricula, ["formal parameter" and "actual parameter" are long-deprecated terms](https://stackoverflow.com/questions/18870156/what-is-a-formal-parameter#comment27847191_18870232). Prefer "parameter" and "argument" to align with your peers. – Asteroids With Wings Feb 15 '20 at 14:14

4 Answers4

4

How to pass string by reference in C++?

You simply add & to the parameter.

Are strings a normal array?

No, std::string is not an array.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

Reference with & in function parameter, this means you pass a reference to the string you initialize in the main function. Without & you will just be passing a copy and the changes you make in the copy don't reflect in the original.

Also the call should be:

reverseStr(str, 0, 2);

Range 0 to 2, or better yet:

reverseStr(str, 0, str.size() - 1);

As you can see by the use of .size() method, an std::string is not a char array, it's a typedef of the class template std::basic_string<char>.

Learn more about what C++ string type is in https://pt.cppreference.com/w/cpp/string

anastaciu
  • 23,467
  • 7
  • 28
  • 53
-1

The syntax for references in C++ is

void reverseStr(string &arr, int start, int end);

You can think about arr as a pointer, which is automatically dereferenced.

More information about references here.

Tarek Dakhran
  • 2,021
  • 11
  • 21
  • OP knows this and is asking _why_ – Asteroids With Wings Feb 15 '20 at 14:12
  • People shouldn't learn C++ from web "tutorials" written by random people on the internet, but from a good book written by widely-respected domain experts. Please don't link to those. Full of shortcuts and errors. – Asteroids With Wings Feb 15 '20 at 14:15
  • Before writing comments like this, try to find a single error in the link I provide. – Tarek Dakhran Feb 15 '20 at 14:17
  • It's a general remark about what sort of educational resource to point beginners to, @TarekD. There doesn't need to be a specific error on that specific article. – Asteroids With Wings Feb 15 '20 at 14:18
  • Okay here's one error _"Pointers can be initialized at any time."_ No, initialization always happens at the point of definition. But you can assign later. – Asteroids With Wings Feb 15 '20 at 14:19
  • 3
    TutorialsPoint should generally be avoided like the plague. It's riddled with inaccurate information. Linking to official documentation or time proven resources such as cppreference is very much preferred. – thomas_f Feb 15 '20 at 14:23
-2

You have to pass by reference because that's how the language is designed.

Your theory that "strings are arrays" is incorrect. Strings are strings.

It is true that, for historical reasons, passing C arrays by value actually appears to pass them by reference (in reality the array's name is decaying to a pointer). But that's not what you have here, and it's not how the rest of the language works.

If you pass by value, usually, you get a copy. So your function operates on a copy. Passing a reference means you operate on the original object, instead.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35