Results 1 to 15 of 15

Thread: char*

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112

    char*

    using char* to store a string isn't c++ standard, right? i am supposed to use the string class, right? thanks for the tip

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    You can use char * to store a C string (A null-terminated char array), but yes, using the string from the standard library is recommended in C++
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112
    PHP Code:
    const char g_szClassName[] = "myWindowClass"
    (this is from the 2nd winprog tutorial..i know its intended to be c )
    having this in a c++ program wouldn't be good, right? but how would i replace it?

    PHP Code:
    string g_sClassName ... 
    ?
    thanks guys

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It's good because it is a constant. Using the string class for a constant is a waste of memory and computation time.

    But it is bad too: it still might waste memory. Since it is a constant it should be a pointer:
    Code:
    const char * const g_szClassName = "myWindowClass";
    This creates an absolutly non-modifyable string.

    And better yet, because windows applications should be written in a generic-character way:
    Code:
    const TCHAR * const g_szClassName = TEXT("myWindowClass");
    If you have any questions about that feel free to ask.

    std::string is supposed to be used for strings that you work with, that get modified, searched etc.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112
    what do you mean by generic-character way? i looked in the headers and saw that TCHAR was a typedef of wchar_t.. is that the wide character type i hear ppl talking about?
    thanks for clarifying..so basically i am 'allowed' to use char* only for constant stuff..otherwise, i use the string class for manupulation..
    got it (i'll look into that TCHAR type)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112
    ohh by the way, if i have

    PHP Code:
    const char* const classname "blahblah"
    how would you define classname? a constant pointer to an array of character? i am asking because i am always confused about this:

    PHP Code:
    charstr "string";

    // is not the same as
    char str[] = "string"
    i know the second one is an array of characters..the first one is a pointer to an array of chars?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112
    cornedbee, when would i use the TEXT () macro, and sorry, but i couldn't find any precise info as to what wide character is in the msdn..could you give me some info? thanks a lot for your time

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, one by one.

    First:
    const char* const classname = "blahblah";

    classname is a constant pointer to a constant character string stored directly in your exe file.

    Second:
    char* str = "string";
    char str[] = "string";

    The first is a pointer to a location in your exe file where the string "string" is stored. Because the location in your exe is read-only the pointer should be const:
    const char *str = "string";
    The second is a character array that gets initialized with the string "string", this means it is a 7-element array with the character values 's', 't', 'r', 'i', 'n', 'g' and '\0'.

    About wchar_t: wchar_t (or WCHAR or whatever) is a data type that stores wide characters, characters from the UNICODE UTF-16 standard. Windows NT, 2k and XP internally work only with wide characters. If you pass an ANSI (char) string to the API it gets converted to UNICODE. Any results are converted back.
    Windows 9x and ME don't know UNICODE properly unless you install the UNICODE layer, which does the opposite of the NT ANSI layer.
    WinCE finally knows only UNICODE and can't work with ANSI.

    To sum up: NT works with both character types, but is faster with UNICODE. 9x only works with ANSI unless you install additional DLLs. CE works only with UNICODE.

    Now this is a mess. What character type to use? The solution is the generic character type TCHAR. Depending on the macro UNICODE being defined this type equals either wchar_t or char. By always using TCHAR you can compile for any character type simply by defining or undefining UNICODE.
    There's a problem however. In C source something like this:
    Code:
    "Some String"
    will always be seen as ANSI string by the compiler. Now when you do
    Code:
    const TCHAR *szString = "Hello, World!";
    then it compiles fine as ANSI, but as UNICODE you'll get an error like
    "typecast: Cannot implicitly convert from const char * to const wchar_t *. Conversion requires a reinterpret_cast or C-style cast."

    To get a UNICODE string literal you have to prepend an uppercase L:
    Code:
    const wchar_t *szWideString = L"This is wide.";
    But this is not generic. That's what the TEXT macro is for. Enclose all literal strings in the TEXT macro and they'll be UNICODE when you need UNICODE and ANSI when you need ANSI.

    If you include <tchar.h> (only MSVC++) you can also use the shorter _T macro instead of TEXT.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    112
    thanks a lot CornedBee for your extensive reply! its much clearer now

  10. #10
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    CB,

    const char *str = "string";
    Would not make the value const rather than the pointer?

    Should it not be:
    char* const str = "string";
    ****

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's the point. The string is what's supposed to be const (it's stored in read-only memory). The pointer itself is stored as any other stack variable, and you're free to reassign it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    const char *
    makes the value const
    char * const
    makes the pointer const.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    Originally posted by CornedBee

    Because the location in your exe is read-only the pointer should be const:
    const char *str = "string";
    CB,

    Thanks, I was confused by what you said above.
    ****

  14. #14
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64
    or you can write your own stringclass and it ain't that difficult

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's useless. For constants even the simplest class poses unnecessary overhead, and for non-const strings std::string is almost guaranteed to be better than a string class you write.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width