Whats wrong with this statement?

This is a discussion on Whats wrong with this statement? within the C++ Programming forums, part of the General Programming Boards category; The following statement compiles perfectly with g++. But the sun compiler gives me a warning: "Warning: String literal converted to ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    Whats wrong with this statement?

    The following statement compiles perfectly with g++.
    But the sun compiler gives me a warning:
    "Warning: String literal converted to char* in initialization."

    char *bstr =
    "ABCDEFGHIJKLMNOPQ"
    "RSTUVWXYZabcdefgh"
    "ijklmnopqrstuvwxy"
    "z0123456789+/";

    What is the meaning of this warning? Isnt this statement legal?
    Is it not as per coding standards?

    Thanks.
    lawina

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    That's a strange way to init a string literal but I honestly don't know if it is dangeous or "incorrect" why not be safe and just make it one long string
    Woop?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Change char *bstr to char bstr[], or better yet string bstr(/* string literal*/);

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    "strings" are const, so perhaps it's really moaning about the loss of const in the assignment.

    So perhaps
    const char *bstr = "foo";
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    I believe that is because a string literal is a const char*, so you should declare the null terminated string const char* as well.
    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

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    12
    There is not absolutely nothing to worry. String literals in C/C++ are always null terminated char* const. Two continius string literals concaternated in one "ABC" "DEF" -> "ABCDEF"
    Maybe your compiler considers it const char* const. See combilers documentation about this message.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    String literals in C/C++ are always null terminated char* const
    Did a check of the C++ Standard, and in section 2.13.4, it states:
    "An ordinary string literal has type "array of n const char" and static storage duration, where n is the size of the string as defined below, and is initialized with the given characters."

    The "as defined below" part is:
    "The size of a wide string literal is the total number of escape sequences, universal-character-names, and other characters, plus one for the terminating L'\0'. The size of a narrow string literal is the total number of escape sequences and other characters, plus at least one for the multibyte encoding of each universal-character-name, plus one for the terminating '\0'."

    From what I understand, a const char[n] array decays to a const char* pointer, not a char* const pointer.
    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
    Apr 2006
    Posts
    20
    I managed to get a similar warning using g++ with the option -Wwrite-strings:

    $ g++ -o test1 test1.cc -Wwrite-strings

    In function `int main()':
    warning: deprecated conversion from string constant to `char*'

    Thanks for all the replies.
    Lawina

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 6
    Last Post: 05-10-2008, 02:11 AM
  2. what is wrong with my if statement
    By joker_tony in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 12:26 AM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 04:06 AM
  4. What's wrong with my success statement??
    By cool_dude07 in forum C Programming
    Replies: 7
    Last Post: 07-21-2007, 11:22 PM
  5. Something wrong with this if statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 05:19 PM

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