how to deal with the translation from "const char * " to "const unsigned char *"?

  • Thread starter =?gb2312?B?wNbA1rTzzOzKpg==?=
  • Start date
M

Michael DOUBEZ

Noah Roberts a écrit :
I think you've missed the point. The compiler will not complain about
code that does this:

char * x = "hello";
x[2] = 't';

You'll only get a warning when you try to run the program...if
then...maybe only when you create your release compile with its
optimizations...maybe a customer will find it.

The standard requires that this is ok. It is undefined, even in C where
it comes from, but there can be no diagnostic. Most people would like
to get the usual error about attempting to assign const to non-const
without a cast, but you don't get that in this one special case. Allows
for all sorts of unfortunate errors.

OK. My mistake.

Yes, it is misleading and only because K&R C (before 1988) does not have
the const keyword.

Michael
 
I

Ian Collins

Noah said:
I think you've missed the point. The compiler will not complain about
code that does this:

char * x = "hello";
x[2] = 't';

You'll only get a warning when you try to run the program...if
then...maybe only when you create your release compile with its
optimizations...maybe a customer will find it.

The standard requires that this is ok. It is undefined, even in C where
it comes from, but there can be no diagnostic. Most people would like
to get the usual error about attempting to assign const to non-const
without a cast, but you don't get that in this one special case. Allows
for all sorts of unfortunate errors.

I don't think "there can be no diagnostic" is correct. A string literal
has a type of const char*, so the compiler may (and in my opinion
should) issue a diagnostic for "char* x = "hello";"
 
D

Default User

Ian said:
Noah Roberts wrote:

I don't think "there can be no diagnostic" is correct. A string
literal has a type of const char*, so the compiler may (and in my
opinion should) issue a diagnostic for "char* x = "hello";"

I was going to mention something about this. I believe it was just poor
wording on Noah's part, he meant "can" meaning "possible", I think.
It's possible there will be no diagnostic, because the standard doesn't
require one.




Brian
 
N

Noah Roberts

Default said:
I was going to mention something about this. I believe it was just poor
wording on Noah's part, he meant "can" meaning "possible", I think.
It's possible there will be no diagnostic, because the standard doesn't
require one.

Actually, I assumed it was explicitly stated not to since none do. I
couldn't find the place in the standard that says it is ok not to
actually.
 
I

Ian Collins

Noah said:
Actually, I assumed it was explicitly stated not to since none do. I
couldn't find the place in the standard that says it is ok not to
actually.

The closest is 2.13.4, which defines the type.

Sun CC issues a warning.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top