I have seen this in the book "C++ for Dummies".
I dont understand: its a pointer of type char. So how is this becoming a string?Code:// declare a string char* szString = "Randy"; cout << "The array is "'" << szString << "'" << endl;
I have seen this in the book "C++ for Dummies".
I dont understand: its a pointer of type char. So how is this becoming a string?Code:// declare a string char* szString = "Randy"; cout << "The array is "'" << szString << "'" << endl;
to be more specific - it is a C-string (that is nul-terminated sequence of characters) and should be declared as const char*
because the actual data "Randy" could be stored in some read-only memory, so programmer should not try to modify it using pointer szString
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thank you Elysia and Vart!
But if a string is char*.
What is string*?
Why not write string* szString = "Randy"; ?
Last edited by Ducky; 01-20-2008 at 09:29 AM.
string* is a pointer to a string class. Remember that string is NOT a string - it is a class encapsulating a C-string.
Doesn't work that way. A string (or C-style string) is char*. When you type "my string", then it's interpreted as char* because that string you type must be stored somewhere in your executable, so the compiler just takes the address to where it's stored.Why not write string* szString = "Randy"; ?
string* on the other hand is a pointer to a string CLASS, and it not a string, so that won't work. You'll get an error about casting char* to string*. Note that if you remove the *, it will compile since std::string has an overloaded operator to accept char*.
Elysia: Remember that string is NOT a string - it is a class encapsulating a C-string.
string str = "Randy" ;
String is not only a class, its a type too, like "char" or "int" , isnt it ?
Or thats what you call an "overloaded operator" i presume.
Last edited by Ducky; 01-20-2008 at 09:55 AM.
std::string is a C++-string
std::string* is a pointer to this string
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
To sound less paradoxical...Remember that string is NOT a string - it is a class encapsulating a C-string.
A std::string object is not a C-style null terminated string.
In this case the overloaded constructor, not assignment operator, would be used.Note that if you remove the *, it will compile since std::string has an overloaded operator to accept char*.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So if i understand well char* is not a pointer ( even though it looks like one to me )
its another name for the type "string" like in "string str = "Randy" ;" .
So ( string str = "Randy" ; ) and ( char* str = "Randy" ; ) is the same thing.
char* is a pointer.So if i understand well char* is not a pointer ( even though it looks like one to me )
Nope.its another name for the type "string" like in "string str = "Randy" ;" .
Nope.So ( string str = "Randy" ; ) and ( char* str = "Randy" ; ) is the same thing.
char* is a pointer to a char. It so happens that it can be a pointer to a null terminated string as well (in which case it points to the first character in the string). This is a built-in feature, part of the core language.
std::string is a class type for strings. You would #include <string> to use them. This is part of the C++ standard library.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Laserlight
"It so happens that it can be a pointer to a null terminated string as well (in which case it points to the first character in the string). This is a built-in feature, part of the core language."
I think this one they forget to mention in the books, the authors take it granted though its not
granted for a beginner. Thats why its confusing and hard to understand.
Thank you Laserlight and everybody!
Last edited by Ducky; 01-20-2008 at 12:23 PM.
This is a special case use for pointers that is usually introduced by older books before pointers are introduced. That's probably why the details aren't mentioned. More modern and better books introduce the string class first and deal with pointers when it's time to deal with pointers.