Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > warning for 'char* data = "some string" '

Reply
Thread Tools

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

 
 
mthread
Guest
Posts: n/a
 
      12-01-2008
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 .

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

 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 3:19 pm, mthread <(E-Mail Removed)> 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 .


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 */
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 3:35*pm, maverik <(E-Mail Removed)> 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)
 
Reply With Quote
 
maverik
Guest
Posts: n/a
 
      12-01-2008
On Dec 1, 7:08*pm, Pete Becker <(E-Mail Removed)> wrote:
> On 2008-12-01 07:35:05 -0500, maverik <(E-Mail Removed)> 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)

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-02-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WARNING! Prosoftstore.com is a SCAM! WARNING! pentologer@gmail.com ASP .Net Web Services 0 07-08-2007 10:03 AM
warning C4267 and warning C4996 B. Williams C++ 17 10-27-2006 09:41 PM
Untraceable warning: "Warning: Use of "require" without parenthesesis ambiguous at (eval 23) line 1." Julian Mehnle Perl Misc 17 05-18-2006 11:00 AM
Warning: WARNING Charles Computer Support 7 08-16-2005 09:07 PM
Re: A code fix for MSVC warning C4267 (64-bit compatibility warning,e.g. Boost Spirit) Pete Becker C++ 0 02-10-2005 01:13 PM



Advertisments