In C++, when you return a string, it is returned as a C++ string. It’s important to understand that a string in C++ is not just a string, but a C++ string. A string is how you store data that can be used to create a string object that can be passed around for a different purpose. When we pass a string to the function, the function returns a C++ string that contains that string.
The first step of the C string function is to cast the result as a char*. It is then the function’s responsibility to convert that C string to a char*. The function then returns the original C string. After that, you can use the function to set up a buffer for storing data you want to pass around.
The return value of a function is not an immutable value. Because we returned a string, we now have a copy of the string, and the function could change it. What this means is that we can change the contents of our string to whatever we want, but if we do, then we lose the original string. There are no guarantees that the original string will remain constant unless we also return a C string at the same time.
The C++ standard doesn’t provide any guarantees about the behavior of your returned string, so this is a very common programming error. Fortunately, c++ also makes a bunch of really useful string functions that you can use to pass around stuff, so you usually won’t have to worry about this anymore.
Like, there is a string function that you can use, but you need to call it with a pointer to the string to return; and there is a string function that you can use, but you need to call it with a pointer to the string to return. The C standard doesnt give you any guarantees about the behavior of your returned string, so this is a very common programming error.
There are other string functions, but they all give you a pointer to a string.
It’s worth pointing out that this code is also one of the most dangerous kinds of ‘invalid’ C++ programs. If you make this mistake, chances are that you will be bitten by the compiler’s inability to properly interpret your code.
But the C standard also gives us a way to return a string from a function. For example, we can return a pointer to a string with a C string function, then return a pointer to a string with a C cast function. With all of this, though, it gets difficult to tell if the returned string is valid.
The source code for the C++ application you’re currently using is just a bunch of files and links. It’s all taken up by the compiler for you.
To help you out here, here are some pointers. If you are using Visual Studio (C++) you have the header file included before your code. If you are using the ANSI C compiler, then you have the #include statements included before your code. With these pointers I hope it will all make more sense.