简体   繁体   English

下面的C编程语言是什么意思?

[英]What does the following mean apropos C programming language?

A book on C programming says,一本关于 C 编程的书说,

"There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals. “不幸的是,还有另一类只读对象不受其类型的保护而不会被修改:字符串文字。

Takeaway - String literals are read-only.要点 - 字符串文字是只读的。

If introduced today, the type of string literals would certainly be char const[], an array of const-qualified characters.如果今天引入,字符串文字的类型肯定是 char const[],一个 const 限定字符数组。 Unfortunately, the const keyword was introduced to the C language much later than string literals, and therefore it remained as it is for backward compatibility."不幸的是,const 关键字被引入 C 语言的时间比字符串文字晚得多,因此它保持不变是为了向后兼容。”

Question 1. How can strings be read only, like takeaway says, if they can be modified?问题 1. 如果字符串可以修改,如何像外卖说的那样只读?

Question 2. "There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals."问题 2。“不幸的是,还有另一类只读对象不受其类型保护而不会被修改:字符串文字。”

What type is this referring to, which doesn't keep a string literal from being modified?这指的是什么类型,它不会阻止字符串文字被修改?

Question 3. If string literals were introduced today and had the type char const[], how'll they be an array ie I can't grasp as to how string literals will be an array of const qualified characters?问题 3. 如果今天引入了字符串文字并具有 char const[] 类型,那么它们将如何成为一个数组,即我无法理解字符串文字将如何成为 const 限定字符的数组?

How can strings be read only, like takeaway says, if they can be modified?如果可以修改字符串,如何像外卖所说的那样只读?

Because Unfortunately, the const keyword was introduced to the C language much later than string literals, and therefore it remained as it is for backward compatibility .因为Unfortunately, the const keyword was introduced to the C language much later than string literals, and therefore it remained as it is for backward compatibility

String literals existed before protection (in the form of const keyword) existed in the language.在语言中存在保护(以const关键字的形式)之前存在字符串文字。

Ergo, they are not protected, because the tools to protect it did not exist.因此,它们不受保护,因为不存在保护它的工具。 A classic example of undefined behavior is writing to a string literal, see Undefined, unspecified and implementation-defined behavior .未定义行为的典型示例是写入字符串文字,请参阅未定义、未指定和实现定义的行为

What type is this referring to, which doesn't keep a string literal from being modified?这指的是什么类型,它不会阻止字符串文字被修改?

It has the type char[N] - "array of N char s" - where N is the number of characters in the string plus one for zero terminating character.它的类型为char[N] - “N 个char的数组” - 其中N是字符串中的字符数加上零终止字符加一。

See https://en.cppreference.com/w/c/language/string_literal .请参阅https://en.cppreference.com/w/c/language/string_literal

char *str = "string literal";
      // ^^^ - implicit decay from `char [N]` -> `char *`
str[0] = 'a'; // compiler will be fine, but it's invalid code

// or super shorter form:
"string literal"[0] = 'a'; // invalid code

If string literals were introduced today and had the type char const[], how'll they be an array ie I can't grasp as to how string literals will be an array of const qualified characters?如果今天引入了字符串文字并具有 char const[] 类型,那么它们将如何成为一个数组,即我无法理解字符串文字将如何成为 const 限定字符的数组?

The type would be const char[N] - an array of N constant char s, which means you can't modify the characters.类型将是const char[N] - N 个常量char的数组,这意味着您不能修改字符。

// assuming string literal has type const char [N]

const char *str = "string literal";
            // ^^^ - implicit decay from `const char [N]` -> `const char *`
str[0] = 'a'; // compile time error

// or super shorter form:
"string literal"[0] = 'a'; // with `const char [N]` would be a compiler time error

With gcc compiler use -Wwrite-strings to protect against mistakes like writing to string literals.使用 gcc 编译器使用-Wwrite-strings来防止错误,例如写入字符串文字。

Question 1. How can strings be read only, like takeaway says, if they can be modified?问题 1. 如果字符串可以修改,如何像外卖说的那样只读?

The text does not say they can be modified.文本没有说它们可以修改。 It says they are not protected from being modified.它说它们不受保护而不会被修改。 That is a slight error;这是一个小错误; properly, it should say they are not protected from attempts to modify them: The rules of the C standard do not prevent you from writing code that attempts to modify a string literal, and they do not define the results when a program executes such an attempt.正确地,应该说它们不受保护以防止尝试修改它们:C 标准的规则不会阻止您编写尝试修改字符串文字的代码,并且它们不会在程序执行此类尝试时定义结果. In some circumstances, attempting to modify a string literal may result in a signal, usually ending program execution by default.在某些情况下,尝试修改字符串文字可能会产生一个信号,通常默认情况下会结束程序执行。 In other circumstances, the attempt may succeed, and the string literal will be modified.在其他情况下,尝试可能会成功,并且字符串文字将被修改。 In other circumstances, nothing will happen;在其他情况下,什么都不会发生; there will be neither a signal nor a change to the string literal.字符串文字既没有信号也没有变化。 It is also possible other behaviors may occur.也可能发生其他行为。

Question 2. "There is another family of read-only objects that unfortunately are not protected by their type from being modified: string literals."问题 2。“不幸的是,还有另一类只读对象不受其类型保护而不会被修改:字符串文字。”

What type is this referring to, which doesn't keep a string literal from being modified?这指的是什么类型,它不会阻止字符串文字被修改?

Technically, a string literal is a piece of source code that has a character sequence inside quotes, optionally with an encoding prefix.从技术上讲,字符串文字是一段源代码,在引号内有一个字符序列,可以选择带有编码前缀。 During compilation or program execution, an array is generated with the contents of the character sequence and a terminating null character.在编译或程序执行期间,会生成一个包含字符序列内容和终止 null 字符的数组。 For string literals without a prefix, the type of that array is char [] .对于没有前缀的字符串文字,该数组的类型是char [] (If there is a prefix, the type may also be wchar_t [] , char16_t [] , or char32_t , depending on the prefix.) (如果有前缀,则类型也可能是wchar_t []char16_t []char32_t ,具体取决于前缀。)

Colloquially, we often refer to this array as the string literal, even though the array is the thing that results from a string literal (an array in memory) not the actual string literal (in the source code).通俗地说,我们经常将此数组称为字符串文字,即使该数组是由字符串文字(内存中的数组)而不是实际的字符串文字(在源代码中)产生的东西。

The type char [] does not contain const , so it does not offer the protections that const char [] does. char []类型不包含const ,因此它不提供const char []提供的保护。 (Those protections are fairly mild.) (这些保护相当温和。)

Question 3. If string literals were introduced today and had the type char const[], how'll they be an array ie I can't grasp as to how string literals will be an array of const qualified characters?问题 3. 如果今天引入了字符串文字并具有 char const[] 类型,那么它们将如何成为一个数组,即我无法理解字符串文字将如何成为 const 限定字符的数组?

Your confusion here is unclear.你在这里的困惑是不清楚的。 When a string literal appears in source code, the compiler arranges for its contents to be in the memory of the running program.当字符串文字出现在源代码中时,编译器会将其内容安排在正在运行的程序的 memory 中。 Those contents are in memory as an array of characters.这些内容在 memory 中作为字符数组。 If the rules of C were different, the type of that array would be const char [] instead of char [] .如果 C 的规则不同,则该数组的类型将是const char []而不是char []

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

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