Is this legal stuff as per C Standard?

K

Kelvin Moss

Hi group,

In C++ it's undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?
I will appreciate any references to the Standard.

Thanks.
 
R

Robert Harris

Kelvin said:
Hi group,

In C++ it's undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?
I will appreciate any references to the Standard.

Thanks.
Section 6.7.3 says: "If an attempt is made to modify an object defined
with a const-qualified type with non-const qualified type, the behaviour
is undefined."

So if you do it, anything might happen if you try to modify "abc" through s.

Robert
 
P

Peter Ammon

Robert said:
Section 6.7.3 says: "If an attempt is made to modify an object defined
with a const-qualified type with non-const qualified type, the behaviour
is undefined."

That doesn't apply, since "abc" is not const, which is to say, it is not
an object defined with a const qualified type. Notice that the
important part is what the object is defined as, not the type qualifiers
that the referring lvalue has.
So if you do it, anything might happen if you try to modify "abc"
through s.

Robert

Yes, but the undefined behavior is due to the special "can't modify
string literals" rule.

-Peter
 
T

Thomas Stegen

Kelvin said:
const char *s = "abc";
s is a pointer to const char. So isn't "abc" const ?

No. It's a string literal. And you can't modify string literals.
But it's not const.

To get a const string.

const char arr[] = "abc";
const char *ptr = arr;

char *ptr2 = ptr; /*legal, but you still can't change arr through it.*/

In C, const is a strange beast and the semantics of it are seriously
confused. Confused, as in design by confusion.
 
M

Michael Mair

Kelvin said:
const char *s = "abc";
s is a pointer to const char. So isn't "abc" const ?

Please quote the relevant parts of the posts that went before.
"abc" is a string literal. If you have its address, you may
or may not be able to modify it -- so it is in general
safer to treat it as if it was constant/a read-only object.

Cheers
Michael
 
D

Dan Pop

In said:
const char arr[] = "abc";
const char *ptr = arr;

char *ptr2 = ptr; /*legal, but you still can't change arr through it.*/

No way! Code requiring a diagnostic is NEVER legal. ptr and ptr2 are
NOT assignment-compatible types, so a cast is *required*.

Dan
 
A

Andrey Tarasevich

Kelvin said:
In C++ it's undefined behavior if one tries to un-const the constness
of a const variable with const_cast<>.

Not true. UB happens when one tries to actually _modify_ a const object.
A mere attempt to un-const something doesn't cause any UB. This also
holds in C language.
I want to know if the same holds good in C too.
E.g.

const char *s = "abc";
Later, is trying to do (char *) s legal ?

Yes, it is legal. However, doing

((char*)s)[0] = 'A';

(or something of that nature) is illegal.
 
A

Andrey Tarasevich

Kelvin said:
const char *s = "abc";
s is a pointer to const char. So isn't "abc" const ?

It depends on what exactly you mean by this 'const'. String literal
"abc" in C program is represented by an object of type 'char[4]'. Even
though the elements of this array are not const, an attempt to modify
this array leads to undefined behavior.
 
T

Thomas Stegen

Dan said:
const char arr[] = "abc";
const char *ptr = arr;

char *ptr2 = ptr; /*legal, but you still can't change arr through it.*/

No way! Code requiring a diagnostic is NEVER legal. ptr and ptr2 are
NOT assignment-compatible types, so a cast is *required*.

Err, yeah. I meant to put that in there, but somehow I didn't.

Thanks.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top