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

modifying string literal

1)
char* p = "Plato";
p[4] = 'r'; // runtime error

2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;

Jul 22 '05 #1
12 2165
pv**********@gmail.com wrote:
1)
char* p = "Plato";
p[4] = 'r'; // runtime error

2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;


It's not a contradiction. In (1) you're trying to modify the actual
literal, which is a violation. In (2) you modify the array, which is
initialised with a literal. It's a special case of using a literal.

V
Jul 22 '05 #2
pv**********@gmail.com wrote:
1)
char* p = "Plato";
p[4] = 'r'; // runtime error
You are modifying a string literal. String literals are not modifiable
objects in C++.
2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;


In tihs case you ar modifying array 'c'. Array 'c' is a modifiable
object. No error.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3
<pv**********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
1)
char* p = "Plato";
p[4] = 'r'; // runtime error
Undefined behavior. The implementation is permitted to do anything it
likes.

The type of a string literal is really const char*, but you converted it to
char*. This conversion is permitted for C compatibility. However, trying
to change an element of the literal is prohibited. You should have written
this:

const char* p = "Plato";
p[4] = 'r';

and then the compiler would have caught your error at compile time.
2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;


There is no contradiction. Why do you think there is?
Jul 22 '05 #4
Victor Bazarov wrote:
pv**********@gmail.com wrote:
1)
char* p = "Plato";
p[4] = 'r'; // runtime error

2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;

It's not a contradiction. In (1) you're trying to modify the actual
literal, which is a violation. In (2) you modify the array, which is
initialised with a literal. It's a special case of using a literal.


It's not even a special case. You were right up until the last sentence.
Jul 22 '05 #5
Ron Natalie wrote:
...
1)
char* p = "Plato";
p[4] = 'r'; // runtime error

2)
char c[6] = "Plato";
c[4] = 'i';// ok.Why no runtime here?Why is the contradiction?
cout << c << endl;

It's not a contradiction. In (1) you're trying to modify the actual
literal, which is a violation. In (2) you modify the array, which is
initialised with a literal. It's a special case of using a literal.


It's not even a special case. You were right up until the last sentence.


Actually, this use of string literal is often referred to (see
comp.lang.c FAQ, for example) as a special case when array type doesn't
decay to pointer type, i.e. this is the one and only context where array
is actually copyable as a whole (or at least seems to be). Although this
is not very relevant to the OP's question...

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote in message
news:41**********************@news.newshosting.com ...
It's not a contradiction. In (1) you're trying to modify the actual
literal, which is a violation. In (2) you modify the array, which is
initialised with a literal. It's a special case of using a literal.
It's not even a special case. You were right up until the last sentence.


It is, kind of.

char c[] = "abc";

is equivalent to

char c[] = { 'a', 'b', 'c', '\0' };

which doesn't happen without a specific rule to make it happen.
Jul 22 '05 #7
Andrey Tarasevich wrote:

Actually, this use of string literal is often referred to (see
comp.lang.c FAQ, for example) as a special case when array type doesn't
decay to pointer type, i.e. this is the one and only context where array
is actually copyable as a whole (or at least seems to be). Although this
is not very relevant to the OP's question...


I have no clue what this means. There's nothing special here about
string arrays. There are a handful of cases where ALL arrays are treated
as real types rather than implicitly convertd to pointers. The only
thing "special" here is that other than string literals, there are no
array literals.
Jul 22 '05 #8
The type of a string literal is really const char*, but you converted
it to
char*. This conversion is permitted for C compatibility. However,
trying
to change an element of the literal is prohibited.

Though the syntax is
char* p = "Plato";
internally it is
const char* p = "Plato";

OK.

Since it is only internally const char* it gives a runtime error?


Yes there is no contradiction.
char* p = "Plato";
I forgot to see p as const char*.

Jul 22 '05 #9
pv**********@gmail.com wrote:
The type of a string literal is really const char*, but you converted
it to char*.
The type of string literal is 'const char[N]', not 'const char*'.
This conversion is permitted for C compatibility. However,
trying to change an element of the literal is prohibited.
Yes.
Though the syntax is
char* p = "Plato";
internally it is
const char* p = "Plato";

OK.
No. Internally it still is 'char* p'. In general case
const-qualification of an access path is not related to
const-qualification of the object this path leads to.
Since it is only internally const char* it gives a runtime error?
It gives a runtime error because you are trying to modify an
non-modifiable object - string literal. It has nothing to do with the
pointer 'p' itself.
Yes there is no contradiction.
char* p = "Plato";
I forgot to see p as const char*.


'p' is not a 'const char*'. What would really make sense is declare it
as 'const char* p' explicitly instead of tryig to "remember to see" it
as such. Don't use the deprecated 'string literal -> char*' conversion
unless you have a very good reason to do so.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #10
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:g7********************@comcast.com...
The type of a string literal is really const char*, but you converted
it to char*.
The type of string literal is 'const char[N]', not 'const char*'.


Picky, picky. It's really const char[N+1] because of the null terminator.
Jul 22 '05 #11
"Andrew Koenig" <ar*@acm.org> wrote...
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:g7********************@comcast.com...
The type of a string literal is really const char*, but you converted
it to char*.

The type of string literal is 'const char[N]', not 'const char*'.


Picky, picky. It's really const char[N+1] because of the null terminator.


:-) It depends on how you define 'N'. In fact, N in Andrey's answer does
most likely account for the null terminator because the literal "" cannot
have the type 'const char[0]' since zero-sized arrays are not allowed.

V
Jul 22 '05 #12
Andrew Koenig wrote:
...
The type of a string literal is really const char*, but you converted
it to char*.

The type of string literal is 'const char[N]', not 'const char*'.


Picky, picky. It's really const char[N+1] because of the null terminator.
...


Unjustified :) Initially I wanted to say 'const char[N+1]' but then I
thought that in this case I'll probably need to explain what 'N' is. So,
for brevity sake, I just said it's 'const char[N]' and said nothing
about 'N' (it is not really relevant within the context of this topic).

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #13

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

Similar topics

4
by: Ney André de Mello Zunino | last post by:
Hello. The following is a summary of what I have understood from reading Stroustrup's TCPL 3rd Edition's discourse on "string literals" (5.2.2). String literals are of type const char, where n...
6
by: Uenal Mutlu | last post by:
Why is f() giving access violation when called from tf1() or tf2() ? Is this a compiler and/or language bug? void f(char* psz) { *psz = 0; } void tf1() {
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
6
by: Arnshea | last post by:
(apologies for the crosspost) I'm working with an MFC based COM object. From C# I'd like to be able to call a method on the COM object that takes a string array and modifies the contents. Is...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.