473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is this legal stuff as per C Standard?

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.

Nov 14 '05 #1
10 1561
Kelvin Moss wrote:
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
Nov 14 '05 #2
Thanks. I was pretty sure that this would be the case.

Nov 14 '05 #3
Robert Harris wrote:
Kelvin Moss wrote:
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."


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

--
Pull out a splinter to reply.
Nov 14 '05 #4
const char *s = "abc";
s is a pointer to const char. So isn't "abc" const ?

Nov 14 '05 #5
Kelvin Moss wrote:
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.

--
Thomas.
Nov 14 '05 #6

Kelvin Moss wrote:
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
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #7
In <2u*************@uni-berlin.de> Thomas Stegen <th***********@gmail.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
Kelvin Moss wrote:
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.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #9
Kelvin Moss wrote:
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.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #10
Dan Pop wrote:
In <2u*************@uni-berlin.de> Thomas Stegen <th***********@gmail.com> writes:

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.

--
Thomas.
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
6
by: Teddy | last post by:
Hello all simple code below: void foo() { } void foo(int i = 0) { } int main() { //foo(); // call of overloaded 'foo()' is ambiguous
11
by: puzzlecracker | last post by:
is it possible to call nonconst member function on an unnamed temprary? ex: class String{ public: void doSomething(); }; String createString();
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
2
by: millind | last post by:
I have a small issue. I have a html page which i need to print it legal size. My default printer setting is letter. How can i set a script which will print just this page in legal without change...
30
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
6
by: Dave Vandervies | last post by:
I'm writing an abstraction layer over an OS's ability to do non-blocking reads and writes on various things. The basic idea is that the caller will be able to let the I/O happen in the background,...
11
by: not_a_commie | last post by:
So is it legal to copy stuff out of the .NET framework and mix it into your own code? I assume so, since I could call their function directly if I didn't want to avoid the UI library reference in...
36
by: Max | last post by:
This was asked in a C test. Is the following code legal C? Is it legal in C90? C99? #define main() int main #define mainbody () { return 0; } mainbody
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.