Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > the difference between "const char* s" and "char* const s"

Reply
Thread Tools

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

 
 
Gary
Guest
Posts: n/a
 
      08-23-2006
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;
}

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      08-23-2006
Gary schrieb:
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-23-2006
Gary 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.
> 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.

--
Ian Collins.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-23-2006
Gary 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.
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!


 
Reply With Quote
 
Gary
Guest
Posts: n/a
 
      08-23-2006
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.

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      08-23-2006
In article <(E-Mail Removed). com>,
Gary <(E-Mail Removed)> wrote:
>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".

--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
 
Reply With Quote
 
Knemon
Guest
Posts: n/a
 
      08-24-2006
Gary wrote:
> 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.

CBFalconer wrote:
> 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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-24-2006
Gary wrote:
>
> 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:

I wrote:
>> 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.


and what is foul about that?

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-24-2006
Gary wrote:

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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-24-2006
Default User wrote:
> Gary wrote:
>
>> 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)


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

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE maineline address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
the difference between "const char* s" and "char* const s" xgngli@gmail.com C++ 7 08-28-2006 11:25 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
difference between static const and const at the namespace level cppsks C++ 2 02-10-2005 06:20 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments