the difference between "const char* s" and "char* const s"

X

xgngli

Hi all! I've taken some time on learning the difference between
"pointers to const variables" and "const pointer variables". The
question is: in the following code, can we change the contents of the
const pointer (i.e. t)? I got a segmentation fault in the last for
loop. Thanks in advance!

#include <iostream>

using namespace std;

main ()
{
const char* s; // pointer to const variable
s = "hello";
for (; *s; s++)
// *s = 'a'; WRONG! the string is const, so it cannot be
changed
cout << *s << endl;
s = "world"; // OK! reassign s to another string. the pointer is
not const
for (; *s; s++)
cout << *s << endl;

char* const t = "welcome"; // const pointer
int i = 0;
// t = "abcd"; // WRONG! t is read-only
for (; *(t+i); i++)
{
if (*(t+i) == 'o') // ????????? I suspect I can do this, but
why
segmentation fault ??????????
*(t+i) = 'a'; // change "welcome" to "welcame"
cout << *(t+i);
}
cout << endl;
 
J

Jens Theisen

Hi all! I've taken some time on learning the difference between
"pointers to const variables" and "const pointer variables". The
question is: in the following code, can we change the contents of the
const pointer (i.e. t)? I got a segmentation fault in the last for
loop. Thanks in advance!

It's one of the many weird things in C++. The string literal is always
const, but it converts to a non-const char*, because of some historical
reason I presume. You must not write to a string literal.

By the way, you can cast away constness and than write to an object. It
is perfectly legal do this except when the object was defined const in
the first place. String literals fall under that category.

Jens
 
A

Alan Johnson

Hi all! I've taken some time on learning the difference between
"pointers to const variables" and "const pointer variables". The
question is: in the following code, can we change the contents of the
const pointer (i.e. t)? I got a segmentation fault in the last for
loop. Thanks in advance!

#include <iostream>

using namespace std;

main ()
{
const char* s; // pointer to const variable
s = "hello";
for (; *s; s++)
// *s = 'a'; WRONG! the string is const, so it cannot be
changed
cout << *s << endl;
s = "world"; // OK! reassign s to another string. the pointer is
not const
for (; *s; s++)
cout << *s << endl;

char* const t = "welcome"; // const pointer
int i = 0;
// t = "abcd"; // WRONG! t is read-only
for (; *(t+i); i++)
{
if (*(t+i) == 'o') // ????????? I suspect I can do this, but
why
segmentation fault ??????????
*(t+i) = 'a'; // change "welcome" to "welcame"
cout << *(t+i);
}
cout << endl;
From the standard, 2.13.4.2: "[...] The effect of attempting to modify
a string literal is undefined."
 
A

Alan Johnson

Alan said:
Hi all! I've taken some time on learning the difference between
"pointers to const variables" and "const pointer variables". The
question is: in the following code, can we change the contents of the
const pointer (i.e. t)? I got a segmentation fault in the last for
loop. Thanks in advance!

#include <iostream>

using namespace std;

main ()
{
const char* s; // pointer to const variable
s = "hello";
for (; *s; s++)
// *s = 'a'; WRONG! the string is const, so it cannot be
changed
cout << *s << endl;
s = "world"; // OK! reassign s to another string. the pointer is
not const
for (; *s; s++)
cout << *s << endl;

char* const t = "welcome"; // const pointer
int i = 0;
// t = "abcd"; // WRONG! t is read-only
for (; *(t+i); i++)
{
if (*(t+i) == 'o') // ????????? I suspect I can do this, but
why
segmentation fault ??????????
*(t+i) = 'a'; // change "welcome" to "welcame"
cout << *(t+i);
}
cout << endl;
From the standard, 2.13.4.2: "[...] The effect of attempting to modify
a string literal is undefined."

Somehow my reply got partly attached to the quote.

Anyway, consider the consequences if you COULD change a string literal.
char * const p1 = "welcome" ;
p1[4] = 'a' ; // UNDEFINED BEHAVIOR

What should this print? "welcome" or "welcame"
std::cout << "welcome" << std::endl ;
 
X

xgngli

You are right. I cannot change a string literal. It is right if I do
this before the for loop:
char u[] = "welcome";
char* const t = u;
 
F

Frederick Gotham

I got a segmentation fault in the last for loop


The following two programs are equivalent:

Program (1)

int main()
{
"Hello"[2] = 'x';
}


Program (2)

char const __literal1_const[] = "hello";

char (&__literal1)[sizeof __literal1_const] =
const_cast<char(&)[sizeof __literal1_const]>(__literal1_const);

int main()
{
__literal1[2] = 'k';
}
 
J

Jerry Coffin

[ ... ]
The following two programs are equivalent:

True, but probably not for the reason you believe.
Program (1)

int main()
{
"Hello"[2] = 'x';
}

This has undefined behavior because it attempts to modify a string
literal.
Program (2)

char const __literal1_const[] = "hello";

This has undefined behavior because any name with two consecutive
underscores is reserved.

So, the behavior is equivalent, because it's undefined in either case.
OTOH, the two have entirely different sources for their undefined
behavior (yes, I understand what you were trying to get at, but at least
IMO, your explanation obfuscated the point you were trying to make, not
least by adding in unrelated problems).
 
F

Frederick Gotham

Jerry Coffin posted:
Program (2)

char const __literal1_const[] = "hello";

This has undefined behavior because any name with two consecutive
underscores is reserved.


The purpose of my code was to demonstrate what goes on "behind the curtains"
when one uses a string literal in one's code. Therefore, any and all
identifiers which are used will be unique to the implementation.

I thought my code made that obvious.
 

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
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top