Thread: How is it I can change a string literal even if its declared const?

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    4

    How is it I can change a string literal even if its declared const?

    I had an embarrassing moment on SO where a user asked if it was safe to pass a String literal to a function. I answered that passing either a `char*` or `char[]` would create a `char*` on the stack once the function was called. Because of my misunderstanding I said that changing the address of the char* is legal even if its declared `const`. But what I didn't know was changing the char array pointed it pointed to was also legal.

    So I tried myself and lo and behold it works even if the parameter in the function `func()` is `const`! But its illegal to change an element within the array? It just seems inconsistent to me.

    Here is the code I wrote:
    Code:
    #include <stdio.h>
    
    
    void func(const char *x, int* y) {
        printf("func output:\n");
        printf("%p\n", x); 
        printf("%s\n", x); 
        x = "New string"; //how is this legal?
        x = x + 1000; //chage address pointed to by the char*
        printf("%s\n\n", x);
    
        printf("%p\n", y);
        printf("%d\n", *y);
    
        y = y + 10;
        printf("%p\n", y);
        printf("%d\n", *y);
    }
    
    int main(void) {
        const char* ptr = "String";
        int x = 25;
        int * int_ptr = &x;
        func(ptr, int_ptr);
        puts("In main() \n");
        printf("%s\n", ptr);
        ptr = "New string";
        printf("%s\n", ptr); //able to change the string literal. Why?
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > x = "New string"; //how is this legal?
    Because the 'const' refers to what x points to, not x itself.
    Where you position the const is very important!

    char *x;
    Both x= and *x= are permitted.

    const char *x;
    You can do x=, but you can't do *x=

    char * const x;
    You can do *x=, but you can't do x=

    const char * const x;
    Can you figure out this one?


    > x = x + 1000; //chage address pointed to by the char*
    But this isn't, because now x points to out of bounds of the string you assigned.

    > But its illegal to change an element within the array?
    Yes it is, but nowhere in your code do you attempt to do that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The function declared with const issues a warning
    By DevZero in forum C Programming
    Replies: 5
    Last Post: 07-20-2017, 06:05 PM
  2. Change string literal in C through pointer?
    By Maor Elbaz in forum C Programming
    Replies: 7
    Last Post: 12-15-2016, 03:07 PM
  3. Literal ints are non-const?
    By Mozza314 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2011, 11:59 PM
  4. Replies: 3
    Last Post: 10-12-2006, 06:58 AM
  5. const references initialized to a literal
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2006, 08:52 AM

Tags for this Thread