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

Discussion in 'C Programming' started by Gary, Aug 23, 2006.

  1. Gary

    Gary Guest

    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;
    }
     
    Gary, Aug 23, 2006
    #1
    1. Advertising

  2. Gary

    Michael Mair Guest

    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.
     
    Michael Mair, Aug 23, 2006
    #2
    1. Advertising

  3. Gary

    Ian Collins Guest

    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.
     
    Ian Collins, Aug 23, 2006
    #3
  4. Gary

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
     
    CBFalconer, Aug 23, 2006
    #4
  5. Gary

    Gary Guest

    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, Aug 24, 2006
    #5
  6. In article <>,
    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.


    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
     
    Walter Roberson, Aug 24, 2006
    #6
  7. Gary

    Knemon Guest

    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.
     
    Knemon, Aug 24, 2006
    #7
  8. Gary

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
     
    CBFalconer, Aug 24, 2006
    #8
  9. Gary

    Default User Guest

    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)
     
    Default User, Aug 24, 2006
    #9
  10. Gary

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE maineline address!
     
    CBFalconer, Aug 24, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page