gpt4 book ai didi

c++ - char a[] = ?string?; 有什么区别?和 char *p = ?string?;?

转载 作者:IT老高 更新时间:2023-10-28 13:57:43 25 4
gpt4 key购买 nike

正如标题所说,有什么区别

char a[] = ?string?; and 
char *p = ?string?;

这个问题是在面试时问我的。我什至不明白这个说法。

char a[] = ?string?

? 运算符是什么?它是字符串的一部分还是有特定的含义?

最佳答案

? 似乎是一个错字,它在语义上无效。所以答案假设 ? 是一个错字,并解释了面试官可能真正要问的问题。


首先,两者明显不同:

  1. 第一个创建一个指针。
  2. 第二个创建一个数组。

继续阅读以获得更详细的解释:

数组版本:

char a[] = "string";  

创建一个足够大的数组来保存字符串文字“string”,包括它的 NULL 终止符。数组 string 用字符串文字“string”初始化。 数组可以在以后修改。此外,即使在编译时数组的大小也是已知的,因此可以使用 sizeof 运算符来确定其大小。


指针版本:

char *p  = "string"; 

创建一个指向字符串文字“string”的指针。这比数组版本快,但指针指向的字符串不应更改,因为它位于只读实现定义的内存中。修改这样的字符串文字会导致 Undefined Behavior

事实上,C++03 不赞成[Ref 1] 使用不带 const 关键字的字符串字面量。所以声明应该是:

const char *p = "string";

此外,您需要使用 strlen() 函数,而不是 sizeof 来查找字符串的大小,因为 sizeof 运算符只会给你指针变量的大小。


哪个版本更好,我应该使用哪个版本?

取决于用途。

  • 如果您不需要对字符串进行任何更改,请使用指针版本。
  • 如果您打算更改数据,请使用数组版本。

注意:这不是 C++,但这是特定于 C 的。

请注意,在 C 中使用不带 const 关键字的字符串字面量是完全有效的。但是,修改字符串文字仍然是 C[Ref 2] 中的未定义行为。

这就引出了一个有趣的问题,
What is the difference between char* and const char* when used with string literals in C?


对于Standerdese粉丝:
[引用 1]C++03 标准:§4.2/2

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”. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [Example: "abc" is converted to “pointer to const char” as an array-to-pointer conversion, and then to “pointer to char” as a qualification conversion. ]

C++11 只是删除了上面的引用,这意味着它是 C++11 中的非法代码。

[引用 2]C99 标准 6.4.5/5“字符串文字 - 语义”:

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++ - char a[] = ?string?; 有什么区别?和 char *p = ?string?;?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9460260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com