warning for 'char* data = "some string" '

Discussion in 'C++' started by mthread, Dec 1, 2008.

  1. mthread

    mthread Guest

    Hi,
    I am developing a http parser in C++. When I use the statement,
    char* data = "some string";
    I receive the warning,
    'warning : deprecated conversion from sting constant to
    char*'

    kindly let me know why the statement is deprecated. I have just
    upgraded
    my OS(from fedora 8 to fedora 9) and I did not get the warning
    in my previous OS(fedora 8).

    I also have a need to copy the string in character pointer as I
    do all the parsing using the data available in this pointer. I would
    also like to add that this warning is not shown when I use a 'const
    char*'(ie const char* data = "some string" ).
     
    mthread, Dec 1, 2008
    #1
    1. Advertising

  2. mthread

    maverik Guest

    On Dec 1, 3:19 pm, mthread <> wrote:
    > Hi,
    > I am developing a http parser in C++. When I use the statement,
    > char* data = "some string";


    1. Use std::string

    > I receive the warning,
    > 'warning : deprecated conversion from sting constant to
    > char*'


    Ok. Compiler tells you that you try to convert const char* to char*
    because string literal "some string" is type of const char*. Of
    course, in common it's bad to convert const char* to char* because the
    variable of type const char* shouldn't be changed, but using char* you
    can change it.
    So, complier fairly warnings you.

    >
    > kindly let me know why the statement is deprecated. I have just
    > upgraded
    > my OS(from fedora 8 to fedora 9) and I did not get the warning
    > in my previous OS(fedora 8).


    Probably, you complier has been upgraded with new version of distr.

    > I also have a need to copy the string in character pointer as I
    > do all the parsing using the data available in this pointer. I would
    > also like to add that this warning is not shown when I use a 'const
    > char*'(ie const char* data = "some string" ).


    In your case you can try

    char* data = /* memory allocation */;
    strcpy(data, "some string");

    /* usage of data */
    ....

    /* free data */

    Or use std::string:

    std::string data = "some string"; /* Copying */
     
    maverik, Dec 1, 2008
    #2
    1. Advertising

  3. mthread

    maverik Guest

    On Dec 1, 3:35 pm, maverik <> wrote:
    > Ok. Compiler tells you that you try to convert const char* to char*
    > because string literal "some string" is type of const char*. Of
    > course, in common it's bad to convert const char* to char* because the
    > variable of type const char* shouldn't be changed,


    Strictly speaking, the value (of type T) to that pointer points can't
    be changed in case of const T*. It differs from T* const - constant
    pointer (not pointer to constant) where pointer can't be changed (but
    value it points to can be)
     
    maverik, Dec 1, 2008
    #3
  4. mthread

    maverik Guest

    On Dec 1, 7:08 pm, Pete Becker <> wrote:
    > On 2008-12-01 07:35:05 -0500, maverik <> said:
    >
    >
    >
    > > Ok. Compiler tells you that you try to convert const char* to char*
    > > because string literal "some string" is type of const char*. Of
    > > course, in common it's bad to convert const char* to char* because the
    > > variable of type const char* shouldn't be changed, but using char* you
    > > can change it.

    >
    > There's nothing wrong with changing a variable of type const char*. The
    > issue is changing the character data that it points to. Always try to
    > keep this distinction clear. It will save you many headaches.
    >


    See my second post.

    > Strictly speaking, the value (of type T) to that pointer points can't
    > be changed in case of const T*. It differs from T* const - constant
    > pointer (not pointer to constant) where pointer can't be changed (but
    > value it points to can be)
     
    maverik, Dec 1, 2008
    #4
  5. maverik wrote:
    > ...
    >> I receive the warning,
    >> 'warning : deprecated conversion from sting constant to
    >> char*'

    >
    > Ok. Compiler tells you that you try to convert const char* to char*
    > because string literal "some string" is type of const char*.


    Just to nitpick a bit, I'd like to note that string literal "some
    string" has type 'const char[12]', not 'const char*'.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 2, 2008
    #5
    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. wwj
    Replies:
    7
    Views:
    594
  2. wwj
    Replies:
    24
    Views:
    2,568
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    504
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    623
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
Loading...

Share This Page