Is it possible to modify a string of char in C?

前端 未结 9 1221
小蘑菇
小蘑菇 2020-11-22 09:08

I have been struggling for a few hours with all sorts of C tutorials and books related to pointers but what I really want to know is if it\'s possible to change a char point

9条回答
  •  难免孤独
    2020-11-22 10:00

    No, you cannot modify it, as the string can be stored in read-only memory. If you want to modify it, you can use an array instead e.g.

    char a[] = "This is a string";
    

    Or alternately, you could allocate memory using malloc e.g.

    char *a = malloc(100);
    strcpy(a, "This is a string");
    free(a); // deallocate memory once you've done
    

提交回复
热议问题