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