[Solved] Why can’t i change the content of a constant char pointer in C?


  1. It’s const char* p = "C language";, not char* const. They have different meanings: const char* p means that the chars to which p points to, cannot be modified; while char* const p means that the pointer p itself cannot be modified.

  2. *p = "Change"; is not the correct way to change string’s content. Even when the string is not read-only. One way to modify a string’s content is to use: strcpy(p, "Change")

  3. You cannot modify string literals because they are read-only by definition. However, you can declare “char arrays”, and they can be modified:

    char s[] = "C language";
    printf("%s\n", s);
    strcpy(s, "Change");
    printf("%s\n", s);
    

solved Why can’t i change the content of a constant char pointer in C?