Thread: Modifying const string

  1. #1
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9

    Question Modifying const string

    Hi,
    const char chArr[] = "Hello World!!!";
    char *const pch = &chArr[0];

    *pch = 'A'; //Allowed
    chArr[0] = 'B' //Not-Allowed
    Why it possible to change the Value at &chArr[0] using *pch pointing to &chArr[0] even though chArr[0] is readonly..

    Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because pch is a const pointer to a non-const object. If you have compiler warnings enabled, you should get something like "initialization discards qualifiers from pointer target type" for pch (referring to the fact that pch should point to a char, but chArr[0] is a const char).

    Here's the difference: notice you will also get an error for this, post initialization:
    Code:
    pch = NULL;
    Because pch is a const pointer. This is not the same thing as a pointer to a const value.

    Code:
    char *const pch; // const pointer to non-const type
    const char *pch; // non-const pointer to const type
    const char *const pch; // const pointer to const type
    The last two will give you an error for *pch = 'A', and get rid of the discarded qualifier warning, because the types match correctly.

    BTW, the normal way to initialize a pointer to a char array is:
    Code:
    char *pch = &chArr[0]; // confusing and unnecessary
    char *pch = chArr; // better
    Last edited by MK27; 03-12-2012 at 04:41 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because pch is a const pointer to a non-const char. Its value can't be changed, but what it points to can. Move the const to the left of the asterix to make it a pointer to const char.

    In your code, the assignment of *pch has undefined behaviour according to the standard. Among other things, that says programmers should avoid such things, but a compiler is not required to issue a diagnostic (aka error message) in response. Several good modern compilers, however, will issue a warning if you configure to higher warning levels.

    It is a historical anomaly in the C language that the initialisation of pch in your code is not formally a compiler-diagnosable error. In the days before the C standard, when the const keyword did not even exist, allowing an assignment of the form "char *p = "some string literal" was introduced to cater for programmer laziness. A lot of lazy but vocal programmers used that feature in their code so, when const was later introduced (and a string literal was specified to have a const attribute), an exception had to be written into the language in order to avoid breaking a lot of existing code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying a string literal
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 02-15-2011, 12:26 AM
  2. Modifying a string literal.
    By Eman in forum C++ Programming
    Replies: 45
    Last Post: 12-30-2010, 06:37 PM
  3. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  4. modifying const using pointer
    By cc++learner in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2009, 12:38 PM
  5. Modifying reference to a const
    By kishore in forum C++ Programming
    Replies: 18
    Last Post: 01-18-2007, 02:37 PM