char* szString

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

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

    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
    Posts
    22,915
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    std::string is a C++-string
    std::string* is a pointer to this string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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
    22,165
    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.
    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

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    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
    812
    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
    22,165
    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.
    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

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21