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

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

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
7 14641
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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
2
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 ...
9
by: Gary | last post by:
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...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.