Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   assigning "const char *" to "char *" (http://www.velocityreviews.com/forums/t707375-assigning-const-char-to-char.html)

thomas 12-02-2009 12:39 PM

assigning "const char *" to "char *"
 
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?

Balog Pal 12-02-2009 01:11 PM

Re: assigning "const char *" to "char *"
 
"thomas" <freshthomas@gmail.com>
> 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!


Johannes Schaub (litb) 12-02-2009 01:39 PM

Re: assigning "const char *" to "char *"
 
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).

Ian Collins 12-03-2009 05:44 AM

Re: assigning "const char *" to "char *"
 
Balog Pal wrote:
> "thomas" <freshthomas@gmail.com>
>> 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

James Kanze 12-03-2009 09:30 AM

Re: assigning "const char *" to "char *"
 
On Dec 3, 5:44 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Balog Pal wrote:
> > "thomas" <freshtho...@gmail.com>
> >> 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

bnvenkataraman@gmail.com 08-21-2012 11:40 AM

Re: assigning "const char *" to "char *"
 
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

bnvenkataraman@gmail.com 08-21-2012 11:43 AM

Re: assigning "const char *" to "char *"
 
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

Öö Tiib 08-21-2012 01:15 PM

Re: assigning "const char *" to "char *"
 
On Tuesday, August 21, 2012 2:43:37 PM UTC+3, bnvenka...@gmail.com 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*){
}


Victor Bazarov 08-21-2012 01:32 PM

Re: assigning "const char *" to "char *"
 
On 8/21/2012 7:43 AM, bnvenkataraman@gmail.com 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


All times are GMT. The time now is 08:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.