| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
10 Dec 2004 10:52:55 AM |
| Object: |
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;
.
|
|
| User: "Andrew Koenig" |
|
| Title: Re: modifying string literal |
10 Dec 2004 12:41:00 PM |
|
|
<pvinodhkumar@gmail.com> wrote in message
news:1102697575.797142.217770@c13g2000cwb.googlegroups.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?
.
|
|
|
| User: "" |
|
| Title: Re: modifying string literal |
11 Dec 2004 03:15:05 AM |
|
|
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*.
.
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: modifying string literal |
11 Dec 2004 01:08:51 PM |
|
|
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
.
|
|
|
| User: "Andrew Koenig" |
|
| Title: Re: modifying string literal |
11 Dec 2004 01:51:08 PM |
|
|
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:g7ednTCkAs1e2CbcRVn-qQ@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.
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: modifying string literal |
11 Dec 2004 02:00:59 PM |
|
|
"Andrew Koenig" <ark@acm.org> wrote...
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:g7ednTCkAs1e2CbcRVn-qQ@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
.
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: modifying string literal |
11 Dec 2004 03:03:16 PM |
|
|
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
.
|
|
|
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: modifying string literal |
10 Dec 2004 11:02:42 AM |
|
|
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
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: modifying string literal |
10 Dec 2004 03:47:42 PM |
|
|
Victor Bazarov wrote:
pvinodhkumar@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.
.
|
|
|
| User: "Andrew Koenig" |
|
| Title: Re: modifying string literal |
10 Dec 2004 07:00:53 PM |
|
|
"Ron Natalie" <ron@sensor.com> wrote in message
news:41ba16cf$0$1586$9a6e19ea@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.
.
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: modifying string literal |
10 Dec 2004 05:12:04 PM |
|
|
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
.
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: modifying string literal |
10 Dec 2004 07:02:17 PM |
|
|
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.
.
|
|
|
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: modifying string literal |
10 Dec 2004 11:14:23 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|