[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

mthread

12/1/2008 12:20:00 PM

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" ).

5 Answers

maverik

12/1/2008 12:35:00 PM

0

On Dec 1, 3:19 pm, mthread <rjk...@gmail.com> 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

12/1/2008 12:48:00 PM

0

On Dec 1, 3:35 pm, maverik <maverik.m...@gmail.com> 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)

Pete Becker

12/1/2008 4:08:00 PM

0

On 2008-12-01 07:35:05 -0500, maverik <maverik.mail@gmail.com> 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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

maverik

12/1/2008 4:12:00 PM

0

On Dec 1, 7:08 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-12-01 07:35:05 -0500, maverik <maverik.m...@gmail.com> 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)

Andrey Tarasevich

12/2/2008 2:30:00 AM

0

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