I\'m teaching myself C++ and I\'m a bit confused about pointers (specifically in the following source code). But first, I proceed with showing you what I know (and then cont
Taking your questions in order:
"Why is name.c_str() copied into a pointer CopyOfName? Does this mean that all strings are essential pointers? So like string testing = "Hello world"; Is actually a pointer pointing to the memory location where "H" is stored?"
As Yu Hao pointed out in his/her comment, it is important to understand the difference between C++-style strings and C-style strings. In the first case, you are dealing with an "opaque" object, whereas in the latter case you are basically dealing with an "array" of characters.
With C++ string objects, you can use the c_str()
method to get a (pointer to a) C-style array of characters. In C, an array is represented using the pointer to the beginning of the array and then references are achieved by supplying an offset (the index into the array) from that starting address. So the answer to the last question in this bundle is "yes", the pointer to the C-style string is the pointer to the first character, 'H'.
"Next, why is it in the print out statement that CopyOfName is not *CopyOfName? Pointers hold memory addresses?"
Because the operator <<
is overloaded to handle C-strings. The implementation of this method "knows what to do with" the pointer.
Why is name.c_str() copied into a pointer CopyOfName?
"name" is an STL string. This is an object, which is different than a c-string. A c-string is a collection of memory, that holds characters, and has a null termination. So, if you're using STL strings and want to turn them into c-strings, you use .c_str() on it to get the c-string.
CopyOfName contains enough memory to hold name, since it was allocated to hold it.
cout has a TON of different things you can use with <<. It looks like it can take char *'s (which are c-strings) or STL strings. It does not look like it can take pointers to STL strings.
I got kind of confused when you introduced "testing" but I think that you are getting confused between c-strings (which are char *'s) and STL strings, which are objects. Don't feel bad, or give up. This stuff is tricky and takes a while to get.
I would reccomend to try and understand the different between the terms "c-string", "char *", "stl string" and maybe "pointer to stl string" too.
In C, strings are simply arrays of characters. And arrays decay to pointers when used as the argument to a function.
In C++, std::string
is a class. It includes a C-style character array within it, and this is what c_str()
returns. But the string itself is not a pointer, so you can't dereference it; you have to use the c_str()
method to get a pointer to the string contents.
In C, where C++ standard strings didn't exist, char* was the so-called "string." As you noted, it is an array of characters ending in a NULL character. Just about any standard library function which takes a C-style string will take a pointer to said string, for two reasons:
Pointers are not the same as arrays. String literals are immutable, and when you have a pointer to a string literal, you can inspect its contents, but modifying them are undefined behavior. When using this syntax:
char arr[] = "hi there";
The string literal is copied into the array. Because you do not specify a size, the compiler automatically deduces it. The NUL
terminator is automatically added as well. If you specify a size, you must make sure that the buffer can hold the NUL
terminator. Therefore:
char arr[5] = "hello";
is a mistake. If you use the brace initializer syntax:
char arr[5] = { "h", "e", "l", "l", "o" };
This is a mistake because there is no NUL
terminator. If you use strcpy
, a NUL
terminator will be added for you.
std::string
provides two methods of returning a pointer to its underlying contents: data
and c_str
. Pre-C++11, the only difference is data
does not include the NUL
terminator. In C++11, it now does, so their behavior is identical. Because the pointer can easily be invalidated, is not safe to manipulate those pointers. It is also not safe to do char * ptr = str.c_str();
because the lifetime of the array returned by c_str
dies at the semi-colon. You need to copy it into a buffer.
You are asking the right questions as a learner.
Answers:
string
is an object, the c_str()
essentially returns a pointer to the first
character of the string (C Style)string
object
cout
prints the string. Also, C++ is smart enough to determine *testing
is illegal