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
So like
string testing = "Hello world";
Is actually a pointer pointing to the memory location where "H" is stored?
No, above you are have object called string
. It would be true for char* testing = "Hello World"
. As you can see it is even declared as pointer and it points to first character in string - H.
Next, why is it in the print out statement that
CopyOfName
is not*CopyOfName
? Pointers hold memory addresses? Using*CopyOfName
would print out the contents of the memory location. I tried this in code blocks and if the input text was "Hello World." Using*CopyOfName
in the print out statement would just give an "H"
cout take pointer to first character of the string so CopyOfName
is right. In this case it will print every character starting from H until it finds \0 (null character). Strings like "hello" have actually 6 characters - 'h' 'e' 'l' 'l' 'o' '\0'
When you write *CopyOfName
you are dereferencing this pointer and *CopyOfName
is actually only one character
I think what you are doing, and others please correct me if I'm wrong, is that you're copying your string into a dynamic char array. So no you are not copying it into a pointer. The reason a pointer is involved there is because dynamic arrays require pointers to allocate their memory correctly if I am right.
It seems like you understand what C-style strings are, but to summarize, they are just arrays of characters in memory, by convention terminated by a nul character \0
. Usually they are referenced via a char*
pointing to the first letter in the string. When they are printed, typically the characters of the string are printed starting from the first, and printing (or copying, etc.) stops when the \0
terminator is reached.
An std::string
is a class that (typically) wraps a C-style string. This means that a std::string
object (typically) has a private C-style string that is used to implement its functionality. The function std::string::c_str()
returns a pointer to this underlying C-style string.
Let's suppose that char *str;
points to a C-style string. If you attempt to run cout << *str << endl;
, you noticed that only the first character is printed. That is because of C++'s function overloading. The data type of *str
is char
, so the char
version of cout
is called and faithfully prints the single character *str
. For compatibility with C-style strings, the version of cout
that takes a char*
as an argument treats the pointer as a C-style string for printing purposes. If you cout
an int*
, for example, the underlying int
will not be printed.
Edit: Another comment:
The reason that your attempt to dereference an std::string
object failed is that, indeed, it is not a pointer. You could dereference the return value of std::string::c_str()
, and you would get back the first char
of the string.
Related: How is std::string implemented?.