C字符串数组初始化 - 这是可变的吗? [英] C-String array initialization - is this mutable?

查看:153
本文介绍了C字符串数组初始化 - 这是可变的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

修改C字符串常量?

指针const char vs char数组vs std :: string


我知道我可能正在用这个问题打死死马,但我有点困惑,我没有设法找到一个确切的答案对SO或谷歌(我有信心是正确的 - 只是太很多关于C字符串筛选的信息)。此外,我已经标记了C ++,因为这是我感兴趣的,即使我们在谈论C风格的字符串。



在这种情况下: / p>

  char const a * =hello 
char const b [] =goodbye;



我会认为hello和goodbye都是不变的字符串,因为它们来自字符串 - / - >

我已经看到,在这个特殊的情况下,改变hello将是未定义的,而改变goodbye好的,假设你从b数组中去除了const。



我假设字符串在b的情况下是可变的,因为它存储在

在这个例子中,由于某些原因,再见不是一个字符串文字。另外,如果再见不是字符串文字,我可以假定它不是在全局内存中,并且在编译时间之后,唯一的参考是留在用户数组单元格中?

解决方案

第一个创建一个指向字符串文字hello的指针可能存储在程序的可执行映像中的不可写存储器中。



第二个创建一个自动数组 1 (通常,但是那是实现定义的)),并使用字符串goodbye初始化它。它等价于

  char const b [] = {'g','o','o','d' ,'b','y','e',0}; 

因此,goodbye它是一个字符串字符串 char const [8] 并存储在不可写内存中,数组 b 一个自动的 1 数组是不可变的,因为你标记它 const ,但你可以删除 const 从变量声明,使数组的内容可变。您只需使用数组goodbye的内容初始化数组的内容。



您不能修改任何一个,因为它们都是 const char [] ,但第二个可以更改为 char



有关详细信息,请参阅此答案:http://stackoverflow.com/a/9106798/726361






1 正如R. Martinho Fernandes在评论中指出的,语法 T x [] = ... 创建一个静态数组(不是自动的,但是静态的(在可执行映像中,通常是实现定义的))如果它在命名空间范围,并且它只是一个自动数组。


Possible Duplicate:
Modifying C string constants?
Pointer to const char vs char array vs std::string

I know I'm probably beating the dead horse with this question, but I'm a little confused and I haven't managed to find an exact answer on SO or google (that I'm confident is right - there's just too much information on C-strings to sift through). Also, I've tagged it C++ because that's what I'm interested in, even though we're talking about C-style strings specifically.

In this situation:

char const a*  = "hello";
char const b[] = "goodbye";

I would have thought that "hello" and "goodbye" were both immutable strings because they come from string-literals that should decay to a char const*.

I've seen that in this particular case though, changing "hello" would be undefined while changing "goodbye" would be fine, assuming you stripped the constness from the b-array.

I assume that the string is mutable in the case of b due to the fact that its stored in a user-defined array.

Are hello and goodbye different in this case? Is goodbye not a string-literal for some reason given this example. Also, if goodbye isn't a string-literal, can I assume it isn't held in global memory, and the only reference to it after compile time is that which is left in the user-array cells?

解决方案

The first one creates a pointer that points to the string literal "hello", which is probably stored in non-writable memory in the executable image of the program. Even if it isn't, you are not allowed to modify the contents of that array.

The second one creates an automatic array1 (on the stack (usually, but that is implementation-defined)) and initialises it with the string "goodbye". It is equivalent to

char const b[] = {'g', 'o', 'o', 'd', 'b', 'y', 'e', 0};

So while "goodbye" is immutable because it is a string literal which is char const[8] and stored in non-writable memory, the array b is an automatic1 array that is immutable because you marked it const, but you could remove the const from the variable declaration to make the array's contents mutable. You are only initialising the contents of the array with the contents of the array "goodbye".

You are not allowed to modify either of them because they are both const char[], but the second one could be changed to char[] to be mutable, while the first one could not.

See this answer for more info: http://stackoverflow.com/a/9106798/726361


1 As R. Martinho Fernandes pointed out in the comments, the syntax T x[] = ... could also create a static array (not automatic but static (in the executable image usually, but that's implementation defined)) if it is at namespace scope, and it's only an automatic array otherwise.

这篇关于C字符串数组初始化 - 这是可变的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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