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

G

Gary

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.
I wrote the code in c++, but the language is not the point, right? :)
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;
}
 
M

Michael Mair

Gary 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.
I wrote the code in c++, but the language is not the point, right? :)

It is.
There are some subtle differences -- and you cannot expect every
participant of comp.lang.c to get them right. In fact, what we
tell you could be utter crap in C++. Last time I looked, there
was no restrict in C++ and "int new = 5; int delete = new - 42;"
was a problem. And const had a subtly different meaning.

So, please ask the question in comp.lang.c++ or post C code.

<snip: C++ code>

One further request: Please do not use C99 style // line comments
for code when posting; line wrapping may change the meaning of
your code whereas /* */ comments are rather robust in this respect.


Cheers
Michael
 
I

Ian Collins

Gary 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.
I wrote the code in c++, but the language is not the point, right? :)

So why didn't you post to the C++ group?
char* const t = "welcome"; // const pointer
int i = 0;
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"

Even though t isn't a pointer to const char, it is still pointing to a
string literal (your compiler should have issued a warning). Changing a
string literal invokes undefined behaviour.
 
C

CBFalconer

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

I wrote the code in c++, but the language is not the point,
right? :)

If you look closely you will see that this is comp.lang.c, not
c++. The languages are not the same. What is good English is
likely to be execrecable Mandarin.
 
G

Gary

You give those who post their message in wrong place a piece of a good
advise; but you've really got a foul mouth, Charles.
 
W

Walter Roberson

You give those who post their message in wrong place a piece of a good
advise; but you've really got a foul mouth, Charles.

He used the non-existant word "execrecable" and probably
meant "execrable" (deserving to be detested or cursed),
not "excretable".
 
K

Knemon

Gary said:
You give those who post their message in wrong place a piece of a good
advise; but you've really got a foul mouth, Charles.

Gary snipped CBFalconer's comments. There is a reason for this. There
is nothing in CBFalconer's comments that would warrant the idiotic
"you've really got a foul mouth" line. Had Chuck's comments not been
snipped, everyone could see immediately that Gary has a mind full of
fecal matter. For that reason, I have restored the supposed foul comments.
If you look closely you will see that this is comp.lang.c, not
c++. The languages are not the same. What is good English is
likely to be execrecable Mandarin.

Other than misspelling execrable, there is nothing remarkable there. It
is time for Gary to stop being what can be charitably called an asshole.
 
C

CBFalconer

Gary said:
You give those who post their message in wrong place a piece of a
good advise; but you've really got a foul mouth, Charles.

You didn't bother to quote anything, so I will:

and what is foul about that?
 
D

Default User

Gary said:
You give those who post their message in wrong place a piece of a good
advise; but you've really got a foul mouth, Charles.

*plonk*



Brian (hates ungrateful liars)
 
C

CBFalconer

Default said:
*plonk*

Brian (hates ungrateful liars)

I think you are confusing inability to correctly translate a
misspelled word and poor vocabulary with ungrateful lying :)
 

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

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top