字符串文字和非字符串非化合物文字可以修改吗? [英] Can a string literal and a non-string non-compound literal be modified?

查看:132
本文介绍了字符串文字和非字符串非化合物文字可以修改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串文字是左值,这为修改字符串文字打开了大门.

String literals are lvalues, which leaves the door open to modify string literals.

从C入手:

在C源代码中,文字是表示固定值的令牌,该值可以是整数,浮点数,字符或细绳.文字的类型取决于其值和符号. 此处讨论的文字与C99标准中引入的复合文字不同.复合文字是普通的可修改对象,类似于变量.

In C source code, a literal is a token that denotes a fixed value, which may be an integer, a floating-point number, a character, or a string. A literal’s type is determined by its value and its notation. The literals discussed here are different from compound literals, which were introduced in the C99 standard. Compound literals are ordinary modifiable objects, similar to variables.

尽管 C并不严格禁止修改字符串文字,但您不应尝试这样做.一方面,编译器将字符串文字视为常量,可能会将其放置在只读存储器中,在这种情况下,尝试的写操作会导致错误.另外,如果在程序中使用了两个或多个相同的字符串文字,则编译器可能会将它们存储在同一位置,以便在访问另一个字符串时修改一个字符串会导致意外结果.

Although C does not strictly prohibit modifying string literals, you should not attempt to do so. For one thing, the compiler, treating the string literal as a constant, may place it in read-only memory, in which case the attempted write operation causes a fault. For another, if two or more identical string literals are used in the program, the compiler may store them at the same location, so that modifying one causes unexpected results when you access another.

  1. 第一段说"C中的文字表示固定值".

  1. The first paragraph says that "a literal in C denotes a fixed value".

  • 这是否意味着不应修改文字(复合文字除外)?

  • Does it mean that a literal (except compound literals) shouldn't be modified?

由于字符串文字不是复合文字,因此是否应该修改字符串文字?

Since a string literal isn't a compound literal, should a string literal be modified?

这两段是否相互矛盾?我应该如何理解它们?

Do the two paragraphs contradict each other? How shall I understand them?

既不是复合文字也不是字符串文字的文字可以修改吗?

Can a literal which is neither compound literal nor string literal be modified?

推荐答案

来自C标准(6.4.5字符串文字)

From the C Standard (6.4.5 String literals)

7不确定这些数组是否有区别,前提是它们的数组 元素具有适当的值. 如果程序尝试执行以下操作: 修改这样的数组,行为是不确定的.

7 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并不严格禁止修改 字符串文字",而编译器会这样做.因此字符串文字应为 修改了吗?

The second paragraph says that "C does not strictly prohibit modifying string literals" while compilers do. So should a string literal be modified?

然后,编译器不会修改字符串文字.它们可以将相同的字符串文字存储为一个数组.

Then compilers do not modify string literals. They may store identical string literals as one array.

@ o11c 在附件J的评论中(内容丰富)指出可移植性问题,其中已撰写

As @o11c pointed out in a comment in the Annex J (informative) Portability issues there is written

J.5通用扩展名

J.5 Common extensions

1以下扩展名广泛用于 许多系统,但不是所有实现都可移植.这 包含任何可能导致严格遵守的扩展名 程序变为无效会导致实现不合格. 此类扩展的示例包括新关键字,额外的库函数 在标准标头中声明,或在具有名称的预定义宏中声明 不是以下划线开头.

1 The following extensions are widely used in many systems, but are not portable to all implementations. The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.

J.5.5可写字符串文字

J.5.5 Writable string literals

1字符串文字可以修改(在这种情况下,相同的字符串 文字应表示不同的对象)(6.4.5).

1 String literals are modifiable (in which case, identical string literals should denote distinct objects) (6.4.5).

这篇关于字符串文字和非字符串非化合物文字可以修改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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