About character pointers in C

后端 未结 7 995
再見小時候
再見小時候 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:25

    String literals in C are not modifiable. A string literal is a string that is defined in the source code of your program. Compilers will frequently store string literals in a read-only portion of the compiled binary, so really your pmessage pointer is into this region that you cannot modify. Strings in buffers that exist in modifiable memory can be modified using the syntax above.

    Try something like this.

    const char* pmessage = "now is the time";
    
    // Create a new buffer that is on the stack and copy the literal into it.
    char buffer[64];
    strcpy(buffer, pmessage);
    
    // We can now modify this buffer
    buffer[1] = 'K';
    

    If you just want a string that you can modify, you can avoid using a string literal with the following syntax.

    char pmessage[] = "now is the time";
    

    This method directly creates the string as an array on the stack and can be modified in place.

    0 讨论(0)
  • 2020-12-11 08:26

    The "string" literal is defined in read only memory, so you shouldn't be modifying it.

    0 讨论(0)
  • 2020-12-11 08:28

    You can use pointer arithmetic to read from a string literal, but not to write to it. The C Standard forbids modifying string literals.

    0 讨论(0)
  • 2020-12-11 08:29

    If you define a literal of the form:

    char* message = "hello world";
    

    the compiler will treat the characters as constant and may well put them in read-only memory.

    So, it is advisable to use the const keyword so that any attempt to change the literal will be prevent the program from compiling:

    const char* message = "hello world";
    

    I' guessing the reason const on a literal is not enforced as part of the language is just for backwards compatibility with pre-standard versions of C where the const keyword didn't exist. Anybody know any better?

    0 讨论(0)
  • 2020-12-11 08:35

    The string is a constant and cannot be modified. If you want to modify it, you can do:

    char pmessage[] = "now is the time";
    

    This initializes an array of characters (including the \0) instead of creating a pointer to a string constant.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题