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

M

mthread

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

maverik

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 */
 
M

maverik

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)
 
M

maverik

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.
 
A

Andrey Tarasevich

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*.

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top