C 和 C++ 中字符串文字的类型是什么? [英] What is the type of string literals in C and C++?

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

问题描述

C 中字符串文字的类型是什么?是char *还是const char *还是const char * const?

What is the type of string literal in C? Is it char * or const char * or const char * const?

C++ 怎么样?

推荐答案

在 C 中,字符串文字的类型是 char[] - 它不是 const 根据类型,但修改内容是未定义的行为.此外,具有相同内容(或足够多的相同内容)的 2 个不同字符串文字可能会或可能不会共享相同的数组元素.

In C the type of a string literal is a char[] - it's not const according to the type, but it is undefined behavior to modify the contents. Also, 2 different string literals that have the same content (or enough of the same content) might or might not share the same array elements.

来自 C99 标准 6.4.5/5字符串文字 - 语义":

From the C99 standard 6.4.5/5 "String Literals - Semantics":

在翻译阶段 7 中,一个字节或值为零的代码被附加到由一个或多个字符串文字产生的每个多字节字符序列.然后使用多字节字符序列初始化一个静态存储持续时间和长度刚好足以包含该序列的数组.对于字符串文字,数组元素具有 char 类型,并使用多字节字符序列的各个字节进行初始化;对于宽字符串文字,数组元素的类型为 wchar_t,并使用宽字符序列进行初始化...

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters...

如果这些数组的元素具有适当的值,则未指定它们是否不同.如果程序尝试修改这样的数组,则行为未定义.

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

在 C++ 中,普通字符串文字的类型为‘n const char’数组"(来自 2.13.4/1字符串文字").但是在 C++ 标准中有一个特殊情况,它使指向字符串文字的指针很容易转换为非 const 限定的指针(4.2/2数组到指针的转换"):

In C++, "An ordinary string literal has type 'array of n const char'" (from 2.13.4/1 "String literals"). But there's a special case in the C++ standard that makes pointer to string literals convert easily to non-const-qualified pointers (4.2/2 "Array-to-pointer conversion"):

不是宽字符串文字的字符串文字(2.13.4)可以转换为指向字符的指针"类型的右值;可以将宽字符串文字转换为指向 wchar_t 的指针"类型的右值.

A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type "pointer to char"; a wide string literal can be converted to an rvalue of type "pointer to wchar_t".

附带说明 - 因为 C/C++ 中的数组很容易转换为指针,所以字符串文字通常可以在指针上下文中使用,就像 C/C++ 中的任何数组一样.

As a side note - because arrays in C/C++ convert so readily to pointers, a string literal can often be used in a pointer context, much as any array in C/C++.

附加社论:接下来的内容实际上主要是我对 C 和 C++ 标准对字符串文字类型所做选择的基本原理的推测.所以请持保留态度(但如果您有更正或其他详细信息,请发表评论):

Additional editorializing: what follows is really mostly speculation on my part about the rationale for the choices the C and C++ standards made regarding string literal types. So take it with a grain of salt (but please comment if you have corrections or additional details):

我认为 C 标准选择制作字符串文字非常量类型,因为曾经(现在)有太多代码期望能够使用非常量限定的 char 指针指向文字.当 const 限定符被添加时(如果我没记错的话,这是在 ANSI 标准化时间完成的,但是在 K&RC 已经积累了大量现有代码之后很久),如果他们指向字符串字面量只能分配给 char const* 类型而无需强制转换,几乎现有的每个程序都需要更改.不是让标准被接受的好方法...

I think that the C standard chose to make string literal non-const types because there was (and is) so much code that expects to be able to use non-const-qualified char pointers that point to literals. When the const qualifier got added (which if I'm not mistaken was done around ANSI standardization time, but long after K&R C had been around to accumulate a ton of existing code) if they made pointers to string literals only able to be be assigned to char const* types without a cast nearly every program in existence would have required changing. Not a good way to get a standard accepted...

我相信对 C++ 的字符串文字是 const 限定的更改主要是为了支持允许文字字符串更合适地匹配采用char const*"的说法.我认为也有人希望关闭类型系统中的一个感知漏洞,但该漏洞在很大程度上被数组到指针转换中的特殊情况打开了.

I believe the change to C++ that string literals are const qualified was done mainly to support allowing a literal string to more appropriately match an overload that takes a "char const*" argument. I think that there was also a desire to close a perceived hole in the type system, but the hole was largely opened back up by the special case in array-to-pointer conversions.

标准的附录 D 表明不推荐使用字符串字面量 (4.2) 的从常量到非常量限定的隐式转换",但我认为这么多代码仍然会中断,以至于编译器需要很长时间实施者或标准委员会实际上愿意拔掉插头(除非可以设计出其他一些聪明的技术 - 但那样漏洞就会回来,不是吗?).

Annex D of the standard indicates that the "implicit conversion from const to non-const qualification for string literals (4.2) is deprecated", but I think so much code would still break that it'll be a long time before compiler implementers or the standards committee are willing to actually pull the plug (unless some other clever technique can be devised - but then the hole would be back, wouldn't it?).

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

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