How to restructure old C initialisation of char* to avoid compiler warnings

Discussion in 'C++' started by tropos, Sep 20, 2006.

  1. tropos

    tropos Guest

    For my sins, I'm maintaining some old C code which is migrated to C++.
    Dozens of lines of it looks like this:

    char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
    " from ("
    " select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
    THEORETICAL_HIGH_VALUE"
    " from %s "
    " where trade_date = (select max(trade_date ) from %s where "
    <snip>
    ;


    The Solaris native C++ compiler complains a lot, saying "Warning:
    String literal converted to char* in initialization." Even if I
    convert the "char *" to "const char *", a lot of the continuation lines
    get that warning. I'd like to get rid of the warnings.

    Being a nice C++ coder, I find a line like

    char * s1 = "something " "and something else" "and more" ;

    pretty wierd. Is this even legitimate C?
    Is there any way to concatenate C strings, upon initialisation of a
    char *, that won't reap a warning?

    Thanks

    Tropos
     
    tropos, Sep 20, 2006
    #1
    1. Advertising

  2. tropos

    Greg Guest

    tropos wrote:
    > For my sins, I'm maintaining some old C code which is migrated to C++.
    > Dozens of lines of it looks like this:
    >
    > char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
    > " from ("
    > " select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
    > THEORETICAL_HIGH_VALUE"
    > " from %s "
    > " where trade_date = (select max(trade_date ) from %s where "
    > <snip>
    > ;
    >
    >
    > The Solaris native C++ compiler complains a lot, saying "Warning:
    > String literal converted to char* in initialization." Even if I
    > convert the "char *" to "const char *", a lot of the continuation lines
    > get that warning. I'd like to get rid of the warnings.
    >
    > Being a nice C++ coder, I find a line like
    >
    > char * s1 = "something " "and something else" "and more" ;
    >
    > pretty wierd. Is this even legitimate C?
    > Is there any way to concatenate C strings, upon initialisation of a
    > char *, that won't reap a warning?


    Change the char * declarations to implicitly-sized character arrays. In
    other words, replace:

    char * s1 = "something " "and something else" "and more" ;

    with

    char s1[] = "something " "and something else" "and more" ;

    That should eliminate the warnings because an array declaration
    effectively allocates writable storage. In other words, a char array is
    suitable for initialization by a constant string literal, whereas a
    char pointer (with no allocated, writable storage of its own) is not.

    Greg
     
    Greg, Sep 20, 2006
    #2
    1. Advertising

  3. tropos

    Earl Purple Guest

    Greg wrote:
    > Change the char * declarations to implicitly-sized character arrays. In
    > other words, replace:
    >
    > char * s1 = "something " "and something else" "and more" ;
    >
    > with
    >
    > char s1[] = "something " "and something else" "and more" ;
    >
    > That should eliminate the warnings because an array declaration
    > effectively allocates writable storage. In other words, a char array is
    > suitable for initialization by a constant string literal, whereas a
    > char pointer (with no allocated, writable storage of its own) is not.
    >
    > Greg


    Beware though what happens afterwards. If you are returning these
    pointers from functions, for example, you will now be returning local
    stack.

    I don't know why you are getting the warnings by putting const in. That
    should fix it.
    The concatenations are perfectly valid in C++ too and have nothing to
    do with the warnings. Such a trick with concatenations is useful when
    you have a long literal string that you want to break over lines.
     
    Earl Purple, Sep 20, 2006
    #3
  4. tropos

    Greg Guest

    Earl Purple wrote:
    > Greg wrote:
    > > Change the char * declarations to implicitly-sized character arrays. In
    > > other words, replace:
    > >
    > > char * s1 = "something " "and something else" "and more" ;
    > >
    > > with
    > >
    > > char s1[] = "something " "and something else" "and more" ;
    > >
    > > That should eliminate the warnings because an array declaration
    > > effectively allocates writable storage. In other words, a char array is
    > > suitable for initialization by a constant string literal, whereas a
    > > char pointer (with no allocated, writable storage of its own) is not.
    > >
    > > Greg

    >
    > Beware though what happens afterwards. If you are returning these
    > pointers from functions, for example, you will now be returning local
    > stack.


    That is a good point, I had assumed that these were global string
    declarations.

    > I don't know why you are getting the warnings by putting const in. That
    > should fix it.
    > The concatenations are perfectly valid in C++ too and have nothing to
    > do with the warnings. Such a trick with concatenations is useful when
    > you have a long literal string that you want to break over lines.


    I believe the issue with the concatenation is that only "adjacent"
    string literals are to be combined. Apparently strings literals not on
    the same line should not be considered adjacent to each other.

    Nonetheless, it seems that most, if not all, C++ compilers do
    concatenate strings on separate lines, but at least some have
    deprecated the feature (hence the warnings). At any rate, adding a
    backslash to the end of each line that evokes a warning will combine
    the line with the one following - essentially creating one very long
    line from the compiler's point of view.

    Greg
     
    Greg, Sep 20, 2006
    #4
  5. tropos posted:

    > Being a nice C++ coder, I find a line like
    >
    > char * s1 = "something " "and something else" "and more" ;
    >
    > pretty wierd.



    It's used all the time. Two string literals place side by side become one;
    the initialisation above is equivalent to:

    char *s1 = "something and something else and more";


    > Is this even legitimate C?



    Legal C89. Legal C99. Legal C++.


    > Is there any way to concatenate C strings, upon initialisation of a
    > char *, that won't reap a warning?



    Just disable the warning. Depending on how "clever" the code is, you might
    run into problems if you change the pointers to "pointer to const".

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 20, 2006
    #5
  6. tropos

    Jack Klein Guest

    On 20 Sep 2006 04:19:11 -0700, "tropos" <>
    wrote in comp.lang.c++:

    > For my sins, I'm maintaining some old C code which is migrated to C++.
    > Dozens of lines of it looks like this:
    >
    > char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
    > " from ("
    > " select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
    > THEORETICAL_HIGH_VALUE"
    > " from %s "
    > " where trade_date = (select max(trade_date ) from %s where "
    > <snip>
    > ;
    >
    >
    > The Solaris native C++ compiler complains a lot, saying "Warning:
    > String literal converted to char* in initialization." Even if I
    > convert the "char *" to "const char *", a lot of the continuation lines
    > get that warning. I'd like to get rid of the warnings.
    >
    > Being a nice C++ coder, I find a line like
    >
    > char * s1 = "something " "and something else" "and more" ;
    >
    > pretty wierd. Is this even legitimate C?
    > Is there any way to concatenate C strings, upon initialisation of a
    > char *, that won't reap a warning?
    >
    > Thanks
    >
    > Tropos


    If your C++ compiler issues that message on this:

    const char *c1 = "Hello, " "World";

    ....it is broken.

    If your C++ compiler does not issue that message on the above, but it
    does issue that message on this:

    const char *c1 = "H"
    "e"
    "l"
    "l"
    "o"
    ","
    " "
    "W"
    "o"
    "r"
    "l"
    "d";

    ....then it is still broken.

    According to the standard, both of these should appear to the
    "compiler" as:

    const char *c1 = "Hello, World"

    In translation phase 6, adjacent string literals are concatenated. It
    isn't until translation phase 7 that preprocessing tokens, which
    include keywords like "const" and "char", are analyzed syntactically
    and semantically.

    So if you modify the code to look like the first or second examples,
    and you get that message from the compiler, your compiler is
    non-conforming. Are you sure there isn't something different about
    the continuation lines that "get that warning" even after you change
    the type of the pointer to const char *?

    Why not copy and post one that generates the warning and one that does
    not?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 21, 2006
    #6
  7. tropos

    Gavin Deane Guest

    Greg wrote:

    > Earl Purple wrote:
    > > Greg wrote:
    > > I don't know why you are getting the warnings by putting const in. That
    > > should fix it.
    > > The concatenations are perfectly valid in C++ too and have nothing to
    > > do with the warnings. Such a trick with concatenations is useful when
    > > you have a long literal string that you want to break over lines.

    >
    > I believe the issue with the concatenation is that only "adjacent"
    > string literals are to be combined. Apparently strings literals not on
    > the same line should not be considered adjacent to each other.


    Who said that?

    #include <string>
    int main()
    {
    std::string s = "this is split"
    "over two lines";
    }

    is perfectly correct. The two string literals are adjacent and are
    concatenated.

    Gavin Deane
     
    Gavin Deane, Sep 21, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerard Flanagan

    From the Tutorial Wiki: suggested restructure

    Gerard Flanagan, Apr 30, 2006, in forum: Python
    Replies:
    0
    Views:
    261
    Gerard Flanagan
    Apr 30, 2006
  2. Tim Clacy
    Replies:
    8
    Views:
    423
    Tim Clacy
    May 30, 2006
  3. Brice Rebsamen
    Replies:
    10
    Views:
    576
    Keith Thompson
    Mar 12, 2008
  4. Fencer
    Replies:
    2
    Views:
    271
    imageguy
    Mar 2, 2009
  5. Rajat Tandon

    "Restructure a Windows application" from scratch in 2 months

    Rajat Tandon, Jan 20, 2005, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    182
    Ajay Kalra
    Jan 20, 2005
Loading...

Share This Page