了解C弦和C ++中的字符串文字 [英] Understanding C-strings & string literals in C++

查看:90
本文介绍了了解C弦和C ++中的字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一些关于字符串文字和C字符串的问题。

I have a few questions I would like to ask about string literals and C-strings.

所以,如果我有这样的事情:

So if I have something like this:

char cstr[] = "c-string";

据我了解,字符串文字是在内存中创建的,带有一个终止的空字节,例如从地址0xA0开始到0xA9结束,然后从那里返回地址和/或将其强制转换为char []类型,然后指向该地址。

As I understand it, the string literal is created in memory with a terminating null byte, say for example starting at address 0xA0 and ending at 0xA9, and from there the address is returned and/or casted to type char [ ] which then points to the address.

执行以下操作是合法的:

It is then legal to perform this:

for (int i = 0; i < (sizeof(array)/sizeof(char)); ++i)
    cstr[i] = 97+i;

从这个意义上讲,只要将字符串文字强制转换为类型,就可以对其进行修改char []?

So in this sense, are string literals able to be modified as long as they are casted to the type char [ ] ?

但是使用常规指针,我已经明白,当它们指向内存中的字符串文字时,它们无法修改内容,因为大多数编译器

But with regular pointers, I've come to understand that when they are pointed to a string literal in memory, they cannot modify the contents because most compilers mark that allocated memory as "Read-Only" in some lower bound address space for constants.

char * p = "const cstring";
*p = 'A'; // illegal memory write

我想我想理解的是为什么不是char *允许像数组那样指向字符串文字的类型并修改其常量?为什么不像使用char []一样将字符串文字转换为char *?如果我在这里有错误的想法或完全不满意,请随时纠正我。

I guess what I'm trying to understand is why aren't char * types allowed to point to string literals like arrays do and modify their constants? Why do the string literals not get casted into char *'s like they do to char [ ]'s? If I have the wrong idea here or am completely off, feel free to correct me.

推荐答案

在第一种情况下,您正在创建一个实际的字符数组,其大小由您要初始化的文字的大小(8 + 1个字节)决定。 cstr 变量在堆栈上分配了内存,字符串文字的 contents (在代码中位于其他位置,可能位于

In the first case you are creating an actual array of characters, whose size is determined by the size of the literal you are initializing it with (8+1 bytes). The cstr variable is allocated memory on the stack, and the contents of the string literal (which in the code is located somewhere else, possibly in a read-only part of the memory) is copied into this variable.

在第二种情况下,局部变量 p 也在堆栈上分配了内存,但是它的内容将是用来初始化它的字符串文字的地址

In the second case, the local variable p is allocated memory on the stack as well, but its contents will be the address of the string literal you are initializing it with.

因此,因为字符串文字可能位于只读存储器中,所以通常不尝试通过 p 指针更改它(您可能会喜欢,或您可能没有)。另一方面,您可以使用 cstr 数组执行任何操作,因为那是您的本地副本,而该副本恰好是从文字中初始化的。

Thus, since the string literal may be located in a read-only memory, it is in general not safe to try to change it via the p pointer (you may get along with, or you may not). On the other hand, you can do whatever with the cstr array, because that is your local copy that just happens to have been initialized from the literal.

(请注意: cstr 变量的类型为 char 数组,在大多数情况下,转换为指向该数组第一个元素的指针可能会例外,例如 sizeof 运算符:此运算符将计算整个数组的大小,而不仅仅是指向第一个元素的指针。)

(Just one note: the cstr variable is of a type array of char and in most of contexts this translates to pointer to the first element of that array. Exception to this may be e.g. the sizeof operator: this one computes the size of the whole array, not just a pointer to the first element.)

这篇关于了解C弦和C ++中的字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆