By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;

Aug 23 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
xg****@gmail.com wrote:
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
Aug 23 '06 #2

P: n/a

xg****@gmail.com wrote:
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."

--
Alan Johnson

Aug 23 '06 #3

P: n/a

Alan Johnson wrote:
xg****@gmail.com wrote:
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 ;

--
Alan Johnson

Aug 23 '06 #4

P: n/a
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;

Aug 23 '06 #5

P: n/a
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';
}

--

Frederick Gotham
Aug 24 '06 #6

P: n/a
In article <un*******************@news.indigo.ie>, fg*******@SPAM.com
says...

[ ... ]
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).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 27 '06 #7

P: n/a
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.

--

Frederick Gotham
Aug 28 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.