简体   繁体   English

关于C中的字符指针

[英]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. 在我看来,pmessage将指向包含这些字符的内存中的连续区域和末尾的'\\0' 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? 那么为什么他们说(K&R)修改个别角色是不确定的?
Moreover, why when I run the following code, I get a "Segmentation Fault"? 此外,为什么当我运行以下代码时,我得到一个“分段错误”?

*(pmessage + 1) = 'K';

String literals in C are not modifiable. C中的字符串文字不可修改。 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. 编译器经常将字符串文字存储在已编译二进制文件的只读部分中,因此实际上您的pmessage指针将进入您无法修改的区域。 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. 这会初始化一个字符数组(包括\\ 0),而不是创建一个指向字符串常量的指针。

You can use pointer arithmetic to read from a string literal, but not to write to it. 您可以使用指针算法从字符串文字中读取,但不能写入它。 The C Standard forbids modifying string literals. C标准禁止修改字符串文字。

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

The literal value of pmessage goes into code, and in most cases they are placed in code memory. pmessage的字面值进入代码,在大多数情况下,它们被放在代码存储器中。 Which is read only 哪个是只读的

When you write: char *pmessage = "now is the time"; 当你写:char * pmessage =“现在是时候”;

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关键字,以便任何更改文字的尝试都将阻止程序编译:

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. 我猜测文本中const的原因并不是作为语言的一部分强制执行,只是为了向后兼容C的预标准版本,其中const关键字不存在。 Anybody know any better? 谁知道更好?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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