assigning "const char *" to "char *"

Discussion in 'C++' started by thomas, Dec 2, 2009.

  1. thomas

    thomas Guest

    void doit(const char* p){
    char *q = p; //ERROR! <1>
    }
    char *p = "abc"; <2>
    -------------------------------------------

    I think both <1> and <2> assign "const char *" to "char *".
    But only <1> causes compile error. Why is <2> legal?
    thomas, Dec 2, 2009
    #1
    1. Advertising

  2. thomas

    Balog Pal Guest

    "thomas" <>
    > void doit(const char* p){
    > char *q = p; //ERROR! <1>
    > }
    > char *p = "abc"; <2>
    > -------------------------------------------
    >
    > I think both <1> and <2> assign "const char *" to "char *".
    > But only <1> causes compile error. Why is <2> legal?


    The standard specifies it (case 2) as a special conversion that is allowed.
    For the sake of compatibility with old C sources. It is deprecated, and
    probably will get removed in the next version.

    Note that despite the conversion is allowad and compiles you must not modify
    the content through p!
    Balog Pal, Dec 2, 2009
    #2
    1. Advertising

  3. thomas wrote:

    > void doit(const char* p){
    > char *q = p; //ERROR! <1>
    > }
    > char *p = "abc"; <2>
    > -------------------------------------------
    >
    > I think both <1> and <2> assign "const char *" to "char *".
    > But only <1> causes compile error. Why is <2> legal?


    It's only in that case of a conversion from a string literal to char* that
    the conversion is allowed - and the conversion is deprecated. The string
    literal has type "char const[3]". In C, such a literal has type "char[3]"
    and when not operand of sizeof, etc, will have type "char*", so this is only
    for compatibility in C++.

    GCC allows additional code to compile, which i think should not be accepted,
    for example:

    char *p = +"abc";

    In that case, you *are* assigning "char const*" to "char*", but not from a
    string literal array to a pointer directly anymore. While GCC diagnoses
    this, it gives a wrong diagnostic (it merely warns about a deprecated
    conversion).
    Johannes Schaub (litb), Dec 2, 2009
    #3
  4. thomas

    Ian Collins Guest

    Balog Pal wrote:
    > "thomas" <>
    >> void doit(const char* p){
    >> char *q = p; //ERROR! <1>
    >> }
    >> char *p = "abc"; <2>
    >> -------------------------------------------
    >>
    >> I think both <1> and <2> assign "const char *" to "char *".
    >> But only <1> causes compile error. Why is <2> legal?

    >
    > The standard specifies it (case 2) as a special conversion that is
    > allowed. For the sake of compatibility with old C sources. It is
    > deprecated, and probably will get removed in the next version.


    A decent compiler should issue a warning, even if it is not required to.

    --
    Ian Collins
    Ian Collins, Dec 3, 2009
    #4
  5. thomas

    James Kanze Guest

    On Dec 3, 5:44 am, Ian Collins <> wrote:
    > Balog Pal wrote:
    > > "thomas" <>
    > >> void doit(const char* p){
    > >> char *q = p; //ERROR! <1>
    > >> }
    > >> char *p = "abc"; <2>
    > >> -------------------------------------------


    > >> I think both <1> and <2> assign "const char *" to "char *".
    > >> But only <1> causes compile error. Why is <2> legal?


    > > The standard specifies it (case 2) as a special conversion
    > > that is allowed. For the sake of compatibility with old C
    > > sources. It is deprecated, and probably will get removed in
    > > the next version.


    > A decent compiler should issue a warning, even if it is not
    > required to.


    FWIW: in practice, even after deprecation, a feature is highly
    unlikely to be removed. On the other hand, deprecation is a
    strong indication from the authors of the standard that a
    warning would be a good thing.

    --
    James Kanze
    James Kanze, Dec 3, 2009
    #5
  6. thomas

    Guest

    On Wednesday, December 2, 2009 6:09:57 PM UTC+5:30, thomas wrote:
    > void doit(const char* p){
    > char *q = p; //ERROR! <1>
    > }
    > char *p = "abc"; <2>
    > -------------------------------------------
    >
    > I think both <1> and <2> assign "const char *" to "char *".
    > But only <1> causes compile error. Why is <2> legal?


    please correct it to
    void doit(const char *p){
    char *q=strdup(p);
    }
    char *p=strdup("abc");
    These will work in new versions.
    compiler version 5.0 is not giving error in old one
    you specified.
    Regards
    , Aug 21, 2012
    #6
  7. thomas

    Guest

    On Wednesday, December 2, 2009 6:09:57 PM UTC+5:30, thomas wrote:
    > void doit(const char* p){
    > char *q = p; //ERROR! <1>
    > }
    > char *p = "abc"; <2>
    > -------------------------------------------
    >
    > I think both <1> and <2> assign "const char *" to "char *".
    > But only <1> causes compile error. Why is <2> legal?


    please correct it to
    void doit(const char *p){
    char *q=strdup(p);
    }
    char *p=strdup("abc");
    These will work in new versions.
    compiler version 5.0 is not giving error in old one
    you specified.
    Regards
    , Aug 21, 2012
    #7
  8. thomas

    Öö Tiib Guest

    On Tuesday, August 21, 2012 2:43:37 PM UTC+3, wrote:
    > On Wednesday, December 2, 2009 6:09:57 PM UTC+5:30, thomas wrote:
    >
    > > void doit(const char* p){

    >
    > > char *q = p; //ERROR! <1>

    >
    > > }

    >
    > > char *p = "abc"; <2>

    >
    > > -------------------------------------------

    >
    > >

    >
    > > I think both <1> and <2> assign "const char *" to "char *".

    >
    > > But only <1> causes compile error. Why is <2> legal?

    >
    >
    >
    > please correct it to
    >
    > void doit(const char *p){
    >
    > char *q=strdup(p);
    >
    > }
    >
    > char *p=strdup("abc");
    >
    > These will work in new versions.
    >
    > compiler version 5.0 is not giving error in old one
    >
    > you specified.


    Bad suggestion. strdup() does malloc() internally so that doit() now leaks memory instead of doing nothing (like it was attempting to do before). Correct fix is

    void doit(const char*){
    }
    Öö Tiib, Aug 21, 2012
    #8
  9. On 8/21/2012 7:43 AM, wrote:
    > On Wednesday, December 2, 2009 6:09:57 PM UTC+5:30, thomas wrote:
    >> void doit(const char* p){
    >> char *q = p; //ERROR! <1>
    >> }
    >> char *p = "abc"; <2>
    >> -------------------------------------------
    >>
    >> I think both <1> and <2> assign "const char *" to "char *".
    >> But only <1> causes compile error. Why is <2> legal?

    >
    > please correct it to
    > void doit(const char *p){
    > char *q=strdup(p);
    > }
    > char *p=strdup("abc");
    > These will work in new versions.
    > compiler version 5.0 is not giving error in old one
    > you specified.


    You didn't answer the question, though. Try to pay attention.

    To the OP:
    It's legal because the Standard says it's legal. So, why does the
    Standard say that? Because of the legacy issues. Once upon a time
    there were no constant data in C. So, plenty of code was written like
    <2>. So, when standardization was under way in C++ as well, it was
    decided to keep the old C code legal in C++, so initialization of
    pointers to non-const char was allowed to be done with string literals.
    If you try to modify those characters, however, you get undefined
    behavior (UB).

    There are tools out there that would warn you about it, but it is NOT an
    error to initialize a pointer to non-const char with a literal.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 21, 2012
    #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,367
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,059
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    806
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,004
    Ian Collins
    May 9, 2006
  5. Peithon

    assigning const char* to char*

    Peithon, Jun 1, 2007, in forum: C Programming
    Replies:
    6
    Views:
    3,453
    Keith Thompson
    Jun 1, 2007
Loading...

Share This Page