Thread: Whats wrong with this statement?

  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,820
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Change char *bstr to char bstr[], or better yet string bstr(/* string literal*/);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    "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.

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

  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
    28,413
    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.
    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
    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, 05: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