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

Discussion in 'C++' started by xgngli@gmail.com, Aug 23, 2006.

  1. 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. 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, 2006
    #1
    1. Advertising

  2. Jens Theisen Guest

    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
     
    Jens Theisen, Aug 24, 2006
    #2
    1. Advertising

  3. Alan Johnson Guest

    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
     
    Alan Johnson, Aug 24, 2006
    #3
  4. Alan Johnson Guest

    Alan Johnson wrote:
    > 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
     
    Alan Johnson, Aug 24, 2006
    #4
  5. Guest

    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 24, 2006
    #5

  6. > 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
     
    Frederick Gotham, Aug 24, 2006
    #6
  7. Jerry Coffin Guest

    In article <unjHg.13032$>,
    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.
     
    Jerry Coffin, Aug 27, 2006
    #7
  8. 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
     
    Frederick Gotham, Aug 29, 2006
    #8
    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.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,248
  2. Santa
    Replies:
    1
    Views:
    1,094
    Mark A. Odell
    Jul 17, 2003
  3. Gary
    Replies:
    9
    Views:
    1,479
    CBFalconer
    Aug 24, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    365
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    267
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page