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;
Printable View
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;
A string is char*.
Basically the compiler puts the string Randy somewhere in your executable and puts the address of the string in your szString pointer.
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
Thank you Elysia and Vart!
But if a string is char*.
What is string*?
Why not write string* szString = "Randy"; ?
std::string is a C++-string
std::string* is a pointer to this string
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.Quote:
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*.
To sound less paradoxical... :DQuote:
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.Quote:
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.
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.Quote:
So if i understand well char* is not a pointer ( even though it looks like one to me )
Nope.Quote:
its another name for the type "string" like in "string str = "Randy" ;" .
Nope.Quote:
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.
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!
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.