Thread: char* szString

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    char* szString

    I have seen this in the book "C++ for Dummies".

    Code:
     
     // declare a string
    char* szString = "Randy";
    cout << "The array is "'" << szString << "'" << endl;
    I dont understand: its a pointer of type char. So how is this becoming a string?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    But if a string is char*.
    What is string*?
    string* is a pointer to a string class. Remember that string is NOT a string - it is a class encapsulating a C-string.

    Why not write string* szString = "Randy"; ?
    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.
    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*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember that string is NOT a string - it is a class encapsulating a C-string.
    To sound less paradoxical...
    A std::string object is not a C-style null terminated string.

    Note that if you remove the *, it will compile since std::string has an overloaded operator to accept char*.
    In this case the overloaded constructor, not assignment operator, would be used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So if i understand well char* is not a pointer ( even though it looks like one to me )
    char* is a pointer.

    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.
    Nope.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on char arrays
    By homeyg in forum C++ Programming
    Replies: 22
    Last Post: 12-23-2004, 04:45 PM