简体   繁体   中英

About character pointers in C

Consider this definition:

char *pmessage = "now is the time";

As I see it, pmessage will point to a contiguous area in the memory containing these characters and a '\\0' at the end. So I derive from this that I can use pointer arithmetic to access an individual character in this string as long as I'm in the limits of this area.

So why they say (K&R) that modifying an individual character is undefined?
Moreover, why when I run the following code, I get a "Segmentation Fault"?

*(pmessage + 1) = 'K';

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.

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.

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

“string”文字在只读内存中定义,因此您不应该修改它。

The literal value of pmessage goes into code, and in most cases they are placed in code memory. Which is read only

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM