assigning "const char *" to "char *"

B

Balog Pal

thomas said:
void doit(const char* p){
char *q = p; //ERROR! <1>
}
char *p = "abc"; <2>

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!
 
J

Johannes Schaub (litb)

thomas said:
void doit(const char* p){
char *q = p; //ERROR! <1>
}
char *p = "abc"; <2>

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

Ian Collins

Balog said:
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.
 
J

James Kanze

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

bnvenkataraman

void doit(const char* p){
char *q = p; //ERROR! <1>
}
char *p = "abc"; <2>

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
 
B

bnvenkataraman

void doit(const char* p){
char *q = p; //ERROR! <1>
}
char *p = "abc"; <2>

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

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*){
}
 
V

Victor Bazarov

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
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top