modifying string literal

P

pvinodhkumar

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;
 
V

Victor Bazarov

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
 
A

Andrey Tarasevich

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.
 
A

Andrew Koenig

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?
 
R

Ron Natalie

Victor said:
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.
 
A

Andrey Tarasevich

Ron said:
...
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...
 
A

Andrew Koenig

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.
 
R

Ron Natalie

Andrey said:
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.
 
P

pvinodhkumar

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*.
 
A

Andrey Tarasevich

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.
 
V

Victor Bazarov

Andrew Koenig said:
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
 
A

Andrey Tarasevich

Andrew said:
...
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).
 

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
474,184
Messages
2,570,979
Members
47,578
Latest member
LC_06

Latest Threads

Top