About character pointers in C

后端 未结 7 1024
再見小時候
再見小時候 2020-12-11 08:02

Consider this definition:

char *pmessage = \"now is the time\";

As I see it, pmessage will point to a contiguous area in the memory contain

7条回答
  •  执念已碎
    2020-12-11 08:41

    When you write: char *pmessage = "now is the time";

    The compiler treats it as if you wrote:

     const char internalstring[] = "now is the time";
     char *pmessage = internalstring;
    

    The reason why you cannot modify the string, is because if you were to write:

     char *pmessage1 = "now is the time";
     char *pmessage2 = "now is the time";
    

    The compiler will treat it as if you wrote:

     const char internalstring[] = "now is the time";
     char *pmessage1 = internalstring;
     char *pmessage2 = internalstring;
    

    So, if you were to change one, you'd change both.

提交回复
热议问题