are immediate strings changing from "char *" to "const char *" ?

Discussion in 'C++' started by Lynn McGuire, Aug 10, 2010.

  1. Lynn McGuire

    Lynn McGuire Guest

    I just upgraded my web server from FreeBSD version 6.2 to version
    7.2. I am now getting the warning message from gcc:
    gen2wkps.cpp:400: warning: deprecated conversion from string constant to 'char*'
    when I compile my server scripts. Line 400 that produced this
    warning is:
    cgiFormInteger ("computerid", &computerID, 0);
    And the prototype is:
    extern cgiFormResultType cgiFormInteger(
    char *name, int *result, int defaultV);

    Are immediate strings changing from "char *" to "const char *" in
    the C++ standard ? Not that it matters a big deal but I am just
    wondering. This will force me to use const more often and think a
    little more about the effects of my programming.

    My version of gcc is:
    Using built-in specs.
    Target: i386-undermydesk-freebsd
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 4.2.1 20070719 [FreeBSD]

    Sincerely,
    Lynn McGuire
     
    Lynn McGuire, Aug 10, 2010
    #1
    1. Advertising

  2. Re: are immediate strings changing from "char *" to "const char *"?

    Lynn McGuire <>, on 10/08/2010 10:47:33, wrote:

    > I just upgraded my web server from FreeBSD version 6.2 to version
    > 7.2. I am now getting the warning message from gcc:
    > gen2wkps.cpp:400: warning: deprecated conversion from string constant to
    > 'char*'
    > when I compile my server scripts. Line 400 that produced this
    > warning is:
    > cgiFormInteger ("computerid", &computerID, 0);
    > And the prototype is:
    > extern cgiFormResultType cgiFormInteger(
    > char *name, int *result, int defaultV);
    >
    > Are immediate strings changing from "char *" to "const char *" in
    > the C++ standard ? Not that it matters a big deal but I am just
    > wondering. This will force me to use const more often and think a
    > little more about the effects of my programming.
    >
    > My version of gcc is:
    > Using built-in specs.
    > Target: i386-undermydesk-freebsd
    > Configured with: FreeBSD/i386 system compiler
    > Thread model: posix
    > gcc version 4.2.1 20070719 [FreeBSD]


    I think you just happen to be working with different compiler options
    than before, as the conversion from a string literal to "char*" -
    instead of their correct type in C++ which is "const char*" - was and
    still is allowed by the standard for compatibility with C, though as a
    deprecated feature that your compiler is warning about.

    If you need to fit a previously existing C interface you can go on using
    the deprecated feature. Coding new stuff in C++ should not take
    advantage of this feature, of course.

    See:
    [conv.array] 4.2p2
    [diff.lex] C.1.1 Clause 2 Subclause _lex.string
    [depr.string] D.4p1

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Aug 10, 2010
    #2
    1. Advertising

  3. Lynn McGuire

    Bo Persson Guest

    Lynn McGuire wrote:
    > I just upgraded my web server from FreeBSD version 6.2 to version
    > 7.2. I am now getting the warning message from gcc:
    > gen2wkps.cpp:400: warning: deprecated conversion from string
    > constant to 'char*' when I compile my server scripts. Line 400
    > that produced this warning is:
    > cgiFormInteger ("computerid", &computerID, 0);
    > And the prototype is:
    > extern cgiFormResultType cgiFormInteger(
    > char *name, int *result, int defaultV);
    >
    > Are immediate strings changing from "char *" to "const char *" in
    > the C++ standard ? Not that it matters a big deal but I am just
    > wondering. This will force me to use const more often and think a
    > little more about the effects of my programming.
    >


    It has been that way all the time. :)

    The conversion from const char* to non-const char* is allowed for
    compatibility with old C code, but was deprecated already in the C++98
    standard. What is new is that the compiler warns about it (and that it
    seems to be forbidden in C++0x).

    Adding more const seems like a good idea.


    Bo Persson
     
    Bo Persson, Aug 10, 2010
    #3
  4. Re: are immediate strings changing from "char *" to "const char *"?

    Francesco S. Carta <>, on 10/08/2010 18:13:51, wrote:

    > Lynn McGuire <>, on 10/08/2010 10:47:33, wrote:
    >
    >> I just upgraded my web server from FreeBSD version 6.2 to version
    >> 7.2. I am now getting the warning message from gcc:
    >> gen2wkps.cpp:400: warning: deprecated conversion from string constant to
    >> 'char*'
    >> when I compile my server scripts. Line 400 that produced this
    >> warning is:
    >> cgiFormInteger ("computerid", &computerID, 0);
    >> And the prototype is:
    >> extern cgiFormResultType cgiFormInteger(
    >> char *name, int *result, int defaultV);
    >>
    >> Are immediate strings changing from "char *" to "const char *" in
    >> the C++ standard ? Not that it matters a big deal but I am just
    >> wondering. This will force me to use const more often and think a
    >> little more about the effects of my programming.
    >>
    >> My version of gcc is:
    >> Using built-in specs.
    >> Target: i386-undermydesk-freebsd
    >> Configured with: FreeBSD/i386 system compiler
    >> Thread model: posix
    >> gcc version 4.2.1 20070719 [FreeBSD]

    >
    > I think you just happen to be working with different compiler options
    > than before, as the conversion from a string literal to "char*" -
    > instead of their correct type in C++ which is "const char*"


    Minor self nit: the correct type is "const char[n]" where "n" is the
    number of effective characters in the string plus one for the appended
    null character - quickly typed in layman terms, see [lex.string] 2.13.4
    for the rigorous definition ;-)

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Aug 10, 2010
    #4
  5. Lynn McGuire

    James Kanze Guest

    On Aug 10, 4:47 pm, Lynn McGuire <> wrote:
    > I just upgraded my web server from FreeBSD version 6.2 to version
    > 7.2. I am now getting the warning message from gcc:
    > gen2wkps.cpp:400: warning: deprecated conversion from string constant to 'char*'
    > when I compile my server scripts. Line 400 that produced this
    > warning is:
    > cgiFormInteger ("computerid", &computerID, 0);
    > And the prototype is:
    > extern cgiFormResultType cgiFormInteger(
    > char *name, int *result, int defaultV);


    > Are immediate strings changing from "char *" to "const char *" in
    > the C++ standard ?


    String literals have always had the type char const[] in C++.
    In the first version of the standard, there was an implicit
    conversion of a string literal (but not other char const* or
    char const[]) to a char*, in order to avoid breaking existing
    code, but this conversion was deprecated from the start.

    --
    James Kanze
     
    James Kanze, Aug 10, 2010
    #5
  6. Re: are immediate strings changing from "char *" to "const char *"?

    On 10/08/2010 18.23, Bo Persson wrote:
    > Lynn McGuire wrote:
    >> I just upgraded my web server from FreeBSD version 6.2 to version
    >> 7.2. I am now getting the warning message from gcc:
    >> gen2wkps.cpp:400: warning: deprecated conversion from string
    >> constant to 'char*' when I compile my server scripts. Line 400
    >> that produced this warning is:
    >> cgiFormInteger ("computerid", &computerID, 0);
    >> And the prototype is:
    >> extern cgiFormResultType cgiFormInteger(
    >> char *name, int *result, int defaultV);
    >>
    >> Are immediate strings changing from "char *" to "const char *" in
    >> the C++ standard ? Not that it matters a big deal but I am just
    >> wondering. This will force me to use const more often and think a
    >> little more about the effects of my programming.
    >>

    >
    > It has been that way all the time. :)
    >
    > The conversion from const char* to non-const char* is allowed for
    > compatibility with old C code, but was deprecated already in the C++98
    > standard.


    The "nice" thing is that more of these conversions have been
    added. See e.g.:

    <http://groups.google.com/group/comp.lang.c++/msg/50079bda59f27c9d>

    --
    Gennaro Prota | name.surname yahoo.com
    Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
    Do you need expertise in C++? I'm available.
     
    Gennaro Prota, Aug 10, 2010
    #6
  7. Lynn McGuire

    Lynn McGuire Guest

    Re: are immediate strings changing from "char *" to "const char *"?

    >> Are immediate strings changing from "char *" to "const char *" in
    >> the C++ standard ?

    >
    > String literals have always had the type char const[] in C++.
    > In the first version of the standard, there was an implicit
    > conversion of a string literal (but not other char const* or
    > char const[]) to a char*, in order to avoid breaking existing
    > code, but this conversion was deprecated from the start.


    I did not know that !

    Thanks,
    Lynn
     
    Lynn McGuire, Aug 10, 2010
    #7
  8. Lynn McGuire

    Lynn McGuire Guest

    Re: are immediate strings changing from "char *" to "const char *"?

    > I think you just happen to be working with different compiler options than before, as the conversion from a string literal to "char*"
    > - instead of their correct type in C++ which is "const char*" - was and still is allowed by the standard for compatibility with C,
    > though as a deprecated feature that your compiler is warning about.


    By upgrading to a newer version of FreeBSD, I got a newer version
    of gcc. Which appears to have a new warning or the warning is now
    turned on at a lower level.

    Thanks,
    Lynn
     
    Lynn McGuire, Aug 10, 2010
    #8
  9. Lynn McGuire

    Lynn McGuire Guest

    Re: are immediate strings changing from "char *" to "const char *"?

    >> It has been that way all the time. :)
    >>
    >> The conversion from const char* to non-const char* is allowed for
    >> compatibility with old C code, but was deprecated already in the C++98
    >> standard.

    >
    > The "nice" thing is that more of these conversions have been
    > added. See e.g.:
    >
    > <http://groups.google.com/group/comp.lang.c++/msg/50079bda59f27c9d>


    Good find ! That is exactly what I was asking but I gained more
    knowledge by asking so that is always good. It would be cool if
    I remember this tomorrow though.

    Thanks,
    Lynn
     
    Lynn McGuire, Aug 10, 2010
    #9
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,484
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,121
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    854
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  5. Gary
    Replies:
    9
    Views:
    1,503
    CBFalconer
    Aug 24, 2006
Loading...

Share This Page