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;
This is a discussion on char* szString within the C++ Programming forums, part of the General Programming Boards category; I have seen this in the book "C++ for Dummies". Code: // declare a string char* szString = "Randy"; cout ...
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.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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
If I have eight hours for cutting wood, I spend six sharpening my axe.
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 08:29 AM.
std::string is a C++-string
std::string* is a pointer to this string
If I have eight hours for cutting wood, I spend six sharpening my axe.
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*.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
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*.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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 08:55 AM.
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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 11:23 AM.
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.