473,394 Members | 1,722 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,394 software developers and data experts.

constant string doubt

hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);
}
----------------------------------

Oct 16 '07 #1
18 1909
sinbad <sinbad.sin...@gmail.comwrote:
hi,
why does the following program gives an runtime error ,
instead of compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);}

----------------------------------
Because you failed to read the FAQ...

http://c-faq.com/decl/strlitinit.html

--
Peter

Oct 16 '07 #2
On Mon, 15 Oct 2007 18:33:52 -0700, sinbad <si***********@gmail.com>
wrote in comp.lang.c:
hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);
}
----------------------------------
Your program produces two different types of undefined behavior.

First you call the variadic function printf() without a prototype in
scope, undefined behavior.

Second you attempt to modify an element of a string literal, which is
specifically undefined behavior as stated by the C standard.

A compiler is never required to issue a diagnostic for undefined
behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 16 '07 #3
On Oct 15, 6:33 pm, sinbad <sinbad.sin...@gmail.comwrote:
hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);}

----------------------------------
'a' is an instance of non-constant pointer to the constant
data"abcdefgh".
so,you are not allowed to change the data using pointers.

Oct 16 '07 #4
naala wrote:
On Oct 15, 6:33 pm, sinbad <sinbad.sin...@gmail.comwrote:
>hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);}

----------------------------------

'a' is an instance of non-constant pointer to the constant
data"abcdefgh".
so,you are not allowed to change the data using pointers.
I don't think you've got that right. Let me give you a counter example -
that program will probably run just fine (for some value of the term
"fine") on some implementations, and on some others it will run fine
(for some value...) if run under the control of the debugger.

The basic issue is discussed in the FAQ at http://www.c-faq.com, I
suggest the original poster goes there for a correct explanation.
Oct 16 '07 #5
"sinbad" <si***********@gmail.coma écrit dans le message de news:
11*********************@e9g2000prf.googlegroups.co m...
hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);
}
----------------------------------
String literals were not made const to avoid breaking a lot of historical C
programs. Yet modifying them invokes undefined behaviour. It is better
practice to use const char * to point to them. gcc for instance has a
special mode where it tries to enforce this:

gcc -Wall -W -O2 -Wwrite-strings test.c

will get the appropriate warning, along with a few others:

test.c: In function `main':
test.c:3: warning: initialization discards qualifiers from pointer target
type
test.c:5: warning: implicit declaration of function `printf'
test.c:6: warning: control reaches end of non-void function

There is still another problem that gcc did not catch: writing to stdout
without a final \n has implementation defined behaviour.

--
Chqrlie.
Oct 16 '07 #6
On Mon, 15 Oct 2007 22:03:30 -0700, naala <ne***********@gmail.com>
wrote in comp.lang.c:
On Oct 15, 6:33 pm, sinbad <sinbad.sin...@gmail.comwrote:
hi,
why does the following program gives an runtime error ,instead of
compilation error. anyone please shed
some light.

thanks
sinbad

------------------------------
int main()
{
char *a = "abcdefgh";
a[1] = 'j';
printf("%s",a);}

----------------------------------

'a' is an instance of non-constant pointer to the constant
data"abcdefgh".
By definition in the C standard, the type of a string literal like
"abcdefgh" in the above program is array of char, and NOT array of
const char. "abcdefgh" is not constant data in C.
so,you are not allowed to change the data using pointers.
Attempting to modify a string literal in C causes undefined behavior.
This is not because the string literal is constant, but because the C
standard specifically states that this is so.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 16 '07 #7
On Tue, 16 Oct 2007 12:03:43 -0500, Jack Klein wrote:
By definition in the C standard, the type of a string literal like
"abcdefgh" in the above program is array of char, and NOT array of const
char. "abcdefgh" is not constant data in C.
It is not const-qualified, but it is constant.
Oct 16 '07 #8
$)CHarald van D)&k wrote:
On Tue, 16 Oct 2007 12:03:43 -0500, Jack Klein wrote:
By definition in the C standard, the type of a string literal like
"abcdefgh" in the above program is array of char, and NOT array of
const char. "abcdefgh" is not constant data in C.

It is not const-qualified, but it is constant.
It may or may not be. It is undefined behavior to attempt to modify the
contents of a string literal.

Brian
Oct 16 '07 #9
On Tue, 16 Oct 2007 19:14:07 +0000, Default User wrote:
$)CHarald van D)&k wrote:
>On Tue, 16 Oct 2007 12:03:43 -0500, Jack Klein wrote:
By definition in the C standard, the type of a string literal like
"abcdefgh" in the above program is array of char, and NOT array of
const char. "abcdefgh" is not constant data in C.

It is not const-qualified, but it is constant.

It may or may not be. It is undefined behavior to attempt to modify the
contents of a string literal.
It's constant in that its value isn't allowed to change without a
specific action by the program, and that all such actions by the program
are not allowed by the language. If you perform such an action anyway,
then sure, it's possible for its value to change, just like it's possible
for the value of a const-qualified object to change.
Oct 16 '07 #10
"Default User" <de***********@yahoo.comwrites:
$)CHarald van D)&k wrote:
[...]
>It's constant in that its value isn't allowed to change without a
specific action by the program,

Ummm, that's true of every variable.
<quibble>It's true of every non-volatile variable.</quibble>

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '07 #11
Keith Thompson wrote:
"Default User" <de***********@yahoo.comwrites:
$)CHarald van D)&k wrote:
[...]
It's constant in that its value isn't allowed to change without a
specific action by the program,
Ummm, that's true of every variable.

<quibble>It's true of every non-volatile variable.</quibble>
Right, I thought about that afterwards.

Brian
Oct 16 '07 #12
$)CHarald van D)&k wrote:

That would be why I'm trying to explain why I believe what I do, and
why I've asked you a question to clarify your position. You have not
answered my question, so I will ask it again: how constant does
something have to be for you to consider it constant?
All you've demonstrated so far is that nothing is a constant. That's
not exactly your going-in position.


Brian
Oct 16 '07 #13
On Tue, 16 Oct 2007 21:07:11 +0000, Default User wrote:
$)CHarald van D)&k wrote:
>That would be why I'm trying to explain why I believe what I do, and
why I've asked you a question to clarify your position. You have not
answered my question, so I will ask it again: how constant does
something have to be for you to consider it constant?

All you've demonstrated so far is that nothing is a constant. That's not
exactly your going-in position.
Quoting myself from a few messages back:

Well, then there are simply no constants in C.

While by my definition of constant, they do exist in C, I also accept
that there are legitimate alternative definitions by which they do not.

But you still haven't answered my question. If you can explain to me
where you draw the line between constants and non-constants, then it
becomes possible to tell whether your interpretation (even while
different from mine) is self-consistent. If it is, then I disagree with
your terminology, but may agree with your conclusions. If it is not, then
I do not agree with either your terminology or your conclusions.
Oct 16 '07 #14
$)CHarald van D)&k wrote:
On Tue, 16 Oct 2007 21:07:11 +0000, Default User wrote:
$)CHarald van D)&k wrote:
That would be why I'm trying to explain why I believe what I do,
and >why I've asked you a question to clarify your position. You
have not >answered my question, so I will ask it again: how
constant does >something have to be for you to consider it constant?

All you've demonstrated so far is that nothing is a constant.
That's not exactly your going-in position.

Quoting myself from a few messages back:

Well, then there are simply no constants in C.
Not, however, your going-in position.
While by my definition of constant, they do exist in C, I also accept
that there are legitimate alternative definitions by which they do
not.

But you still haven't answered my question. If you can explain to me
where you draw the line between constants and non-constants, then it
becomes possible to tell whether your interpretation (even while
different from mine) is self-consistent. If it is, then I disagree
with your terminology, but may agree with your conclusions. If it is
not, then I do not agree with either your terminology or your
conclusions.
Well, one definition would be anything const-qualified plus anything
that is a(n): integer-constant, floating-constant,
enumeration-constant, or character-constant.

Of course, that's not quite what I said orginally, plus you already
know that string literals aren't const-qualified. So with that I would
have to be less waffly and just say that string literals are not
constant at all.


Brian
Oct 16 '07 #15
On Tue, 16 Oct 2007 22:19:55 +0000, Default User wrote:
$)CHarald van D)&k wrote:
>On Tue, 16 Oct 2007 21:07:11 +0000, Default User wrote:
All you've demonstrated so far is that nothing is a constant. That's
not exactly your going-in position.

Quoting myself from a few messages back:

Well, then there are simply no constants in C.

Not, however, your going-in position.
True.
>While by my definition of constant, they do exist in C, I also accept
that there are legitimate alternative definitions by which they do not.

But you still haven't answered my question. If you can explain to me
where you draw the line between constants and non-constants, then it
becomes possible to tell whether your interpretation (even while
different from mine) is self-consistent. If it is, then I disagree with
your terminology, but may agree with your conclusions. If it is not,
then I do not agree with either your terminology or your conclusions.

Well, one definition would be anything const-qualified plus anything
that is a(n): integer-constant, floating-constant, enumeration-constant,
or character-constant.
Thanks. I don't think this definition would lead to contradictions either
way, but out of curiosity, which of the below would qualify as a constant?

const int a = 0;
const int *p = &a;
*p;

const int a = 0;
int *p = (int *) &a;
*p;

int a = 0;
const int *p = &a;
*p;
Of course, that's not quite what I said orginally, plus you already know
that string literals aren't const-qualified. So with that I would have
to be less waffly and just say that string literals are not constant at
all.
With that definition of constant, indeed, they are not. I preferred to
define a constant based on its behaviour and restrictions, but this works
as well.
Oct 17 '07 #16
Harald van D)&k wrote:
On Tue, 16 Oct 2007 22:19:55 +0000, Default User wrote:
Well, one definition would be anything const-qualified plus anything
that is a(n): integer-constant, floating-constant,
enumeration-constant, or character-constant.

Thanks. I don't think this definition would lead to contradictions
either way, but out of curiosity, which of the below would qualify as
a constant?
Now you're going to make me think some more, aren't you? It was hard
enough yesterday. I wasn't expecting a sort of Spanish In. . . no,
better not go there.
const int a = 0;
const int *p = &a;
*p;
Yes. I believe the deference of a const pointer results in a
const-qualified object, although I can't quote any scripture on that.
const int a = 0;
int *p = (int *) &a;
*p;
No. You've "casted away constness", so it's not a constant. Obviously
UB is lurking.
int a = 0;
const int *p = &a;
*p;
Yes. Same reason as 1.
Of course, that's not quite what I said orginally, plus you already
know that string literals aren't const-qualified. So with that I
would have to be less waffly and just say that string literals are
not constant at all.

With that definition of constant, indeed, they are not. I preferred
to define a constant based on its behaviour and restrictions, but
this works as well.
I'm guessing (correct me if wrong) that if attempts to modify an object
result in a constraint violation or UB, then it's a "constant"?


Brian
Oct 17 '07 #17
On Wed, 17 Oct 2007 19:38:28 +0000, Default User wrote:
Harald van D)&k wrote:
>On Tue, 16 Oct 2007 22:19:55 +0000, Default User wrote:
Well, one definition would be anything const-qualified plus anything
that is a(n): integer-constant, floating-constant,
enumeration-constant, or character-constant.

Thanks. I don't think this definition would lead to contradictions
either way, but out of curiosity, which of the below would qualify as a
constant?

Now you're going to make me think some more, aren't you? It was hard
enough yesterday. I wasn't expecting a sort of Spanish In. . . no,
better not go there.
I have no more questions :)
>const int a = 0;
const int *p = &a;
*p;

Yes. I believe the deference of a const pointer results in a
const-qualified object, although I can't quote any scripture on that.
To be precise, the dereference of a pointer-to-const results in an lvalue
expression having some const-qualified type. Whether the object is const-
qualified is again open to interpretation, since there are two types that
are relevant: the effective type, and the type of the expression used to
access it.
>const int a = 0;
int *p = (int *) &a;
*p;

No. You've "casted away constness", so it's not a constant. Obviously UB
is lurking.
Indeed, you're only allowed to read *p, not modify it, even though *p
isn't const-qualified.
>int a = 0;
const int *p = &a;
*p;

Yes. Same reason as 1.
Of course, that's not quite what I said orginally, plus you already
know that string literals aren't const-qualified. So with that I
would have to be less waffly and just say that string literals are
not constant at all.

With that definition of constant, indeed, they are not. I preferred to
define a constant based on its behaviour and restrictions, but this
works as well.

I'm guessing (correct me if wrong) that if attempts to modify an object
result in a constraint violation or UB, then it's a "constant"?
That's very close. If an object's value or effective type can change
without UB, then I do not consider it a constant, and otherwise I do. So
for the above examples, I would consider 1 and 2 constant, rather than 1
and 3. I don't consider example 3 constant, because you're allowed to
modify *p either by casting away the const qualifier, or by modifying a
directly.
Oct 17 '07 #18
$)CHarald van D)&k wrote:
On Wed, 17 Oct 2007 19:38:28 +0000, Default User wrote:
I'm guessing (correct me if wrong) that if attempts to modify an
object result in a constraint violation or UB, then it's a
"constant"?

That's very close. If an object's value or effective type can change
without UB, then I do not consider it a constant, and otherwise I do.
So for the above examples, I would consider 1 and 2 constant, rather
than 1 and 3. I don't consider example 3 constant, because you're
allowed to modify *p either by casting away the const qualifier, or
by modifying a directly.
You didn't cast it away, so that as written it (I think) requires a
diagnostic to modify it through *p. As you say, you can go back to a
itself and back-door modify *p. That's a good point.

Brian


Oct 17 '07 #19

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

Similar topics

11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
15
by: Ramaraj M Bijur | last post by:
Hi All, Could anyone help me to resolve following problem in C the IDE used is Microsoft VC++, Please let me know the which option in VC++ will do the needful The problem statement:...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
18
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further...
3
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5...
3
by: dis_is_eagle | last post by:
Hi.I have a doubt about the following program. int main() { char str="hello"; const char* str1=str; strcpy(str1,"world"); return 0; } I thought as str1 is constant, so copying world to it...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.