字符数组的字面值字符串初始化器

时间:2021-09-28 22:45:12

In the following rules for the case when array decays to pointer:

对于数组衰变为指针的情况,有以下规则:

An lvalue [see question 2.5] of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

在表达式中出现的t的arrayof - t类型的lvalue(见问题2.5)会衰减成指向第一个元素的指针(有三个例外);合成指针的类型是指针对t。

(The exceptions are when the array is the operand of a sizeof or & operator, or is a literal string initializer for a character array.)

(例外情况是,数组是sizeof或& operator的操作数,或者是字符数组的字符串初始化器。)

How to understand the case when the array is "literal string initializer for a character array"? Some example please.

当数组是“字符数组的字符串初始化器”时,如何理解这种情况?请一些示例。

Thanks!

谢谢!

4 个解决方案

#1


40  

The three exceptions where an array does not decay into a pointer are the following:

数组不衰减成指针的三个例外情况如下:

Exception 1. — When the array is the operand of sizeof.

例外1。-当数组为sizeof操作数时。

int main()
{
   int a[10];
   printf("%zu", sizeof(a)); /* prints 10 * sizeof(int) */

   int* p = a;
   printf("%zu", sizeof(p)); /* prints sizeof(int*) */
}

Exception 2. — When the array is the operand of the & operator.

例外2。-当数组是&运算符的操作数时。

int main()
{
    int a[10];
    printf("%p", (void*)(&a)); /* prints the array's address */

    int* p = a;
    printf("%p", (void*)(&p)); /*prints the pointer's address */
}

Exception 3. — When the array is initialized with a literal string.

异常3。-当数组初始化为文字字符串时。

int main()
{
    char a[] = "Hello world"; /* the literal string is copied into a local array which is destroyed after that array goes out of scope */

    char* p = "Hello world"; /* the literal string is copied in the read-only section of memory (any attempt to modify it is an undefined behavior) */
}

#2


7  

Assume the declarations

假设声明

char foo[] = "This is a test";
char *bar  = "This is a test";

In both cases, the type of the string literal "This is a test" is "15-element array of char". Under most circumstances, array expressions are implicitly converted from type "N-element array of T" to "pointer to T", and the expression evaluates to the address of the first element of the array. In the declaration for bar, that's exactly what happens.

在这两种情况下,字符串“This is a test”的类型都是“char的15个元素数组”。在大多数情况下,数组表达式被隐式地从“n元素T数组”转换为“指向T的指针”,表达式计算到数组的第一个元素的地址。在bar的声明中,就是这样。

In the declaration for foo, however, the expression is being used to initialize the contents another array, and is therefore not converted to a pointer type; instead, the contents of the string literal are copied to foo.

然而,在foo的声明中,表达式被用于初始化另一个数组的内容,因此不被转换为指针类型;相反,字符串文本的内容被复制到foo。

#3


5  

This is a literal string initializer for a character array:

这是字符数组的字串初始化器:

char arr[] = "literal string initializer";

Could also be:

也可以:

char* str = "literal string initializer";

Definition from K&R2:

从K&R2定义:

A string literal, also called a string constant, is a sequence of characters surrounded by double quotes as in "...". A string has type ``array of characters'' and storage class static (see Par.A.3 below) and is initialized with the given characters. Whether identical string literals are distinct is implementation-defined, and the behavior of a program that attempts to alter a string literal is undefined.

字符串文字,也称为字符串常量,是由双引号包围的字符序列,如“…”。字符串具有'字符数组'和存储类静态(参见第A段)。,并使用给定的字符初始化。相同的字符串文字是否不同是实现定义的,而试图改变字符串文字的程序的行为是没有定义的。

#4


2  

It seems like you pulled that quote from the comp.lang.c FAQ (maybe an old version or maybe the printed version; it doesn't quite match with the current state of the online one):

看来你是从comp.lang中摘录的。c FAQ(可能是旧版本,也可能是打印版本;它并不完全符合当前在线的状态:

http://c-faq.com/aryptr/aryptrequiv.html

http://c-faq.com/aryptr/aryptrequiv.html

The corresponding section links to other sections of the FAQ to elaborate on those exceptions. In your case, you should look at:

相应的部分链接到FAQ的其他部分来详细说明这些异常。在你的情况下,你应该看看:

http://c-faq.com/decl/strlitinit.html

http://c-faq.com/decl/strlitinit.html

#1


40  

The three exceptions where an array does not decay into a pointer are the following:

数组不衰减成指针的三个例外情况如下:

Exception 1. — When the array is the operand of sizeof.

例外1。-当数组为sizeof操作数时。

int main()
{
   int a[10];
   printf("%zu", sizeof(a)); /* prints 10 * sizeof(int) */

   int* p = a;
   printf("%zu", sizeof(p)); /* prints sizeof(int*) */
}

Exception 2. — When the array is the operand of the & operator.

例外2。-当数组是&运算符的操作数时。

int main()
{
    int a[10];
    printf("%p", (void*)(&a)); /* prints the array's address */

    int* p = a;
    printf("%p", (void*)(&p)); /*prints the pointer's address */
}

Exception 3. — When the array is initialized with a literal string.

异常3。-当数组初始化为文字字符串时。

int main()
{
    char a[] = "Hello world"; /* the literal string is copied into a local array which is destroyed after that array goes out of scope */

    char* p = "Hello world"; /* the literal string is copied in the read-only section of memory (any attempt to modify it is an undefined behavior) */
}

#2


7  

Assume the declarations

假设声明

char foo[] = "This is a test";
char *bar  = "This is a test";

In both cases, the type of the string literal "This is a test" is "15-element array of char". Under most circumstances, array expressions are implicitly converted from type "N-element array of T" to "pointer to T", and the expression evaluates to the address of the first element of the array. In the declaration for bar, that's exactly what happens.

在这两种情况下,字符串“This is a test”的类型都是“char的15个元素数组”。在大多数情况下,数组表达式被隐式地从“n元素T数组”转换为“指向T的指针”,表达式计算到数组的第一个元素的地址。在bar的声明中,就是这样。

In the declaration for foo, however, the expression is being used to initialize the contents another array, and is therefore not converted to a pointer type; instead, the contents of the string literal are copied to foo.

然而,在foo的声明中,表达式被用于初始化另一个数组的内容,因此不被转换为指针类型;相反,字符串文本的内容被复制到foo。

#3


5  

This is a literal string initializer for a character array:

这是字符数组的字串初始化器:

char arr[] = "literal string initializer";

Could also be:

也可以:

char* str = "literal string initializer";

Definition from K&R2:

从K&R2定义:

A string literal, also called a string constant, is a sequence of characters surrounded by double quotes as in "...". A string has type ``array of characters'' and storage class static (see Par.A.3 below) and is initialized with the given characters. Whether identical string literals are distinct is implementation-defined, and the behavior of a program that attempts to alter a string literal is undefined.

字符串文字,也称为字符串常量,是由双引号包围的字符序列,如“…”。字符串具有'字符数组'和存储类静态(参见第A段)。,并使用给定的字符初始化。相同的字符串文字是否不同是实现定义的,而试图改变字符串文字的程序的行为是没有定义的。

#4


2  

It seems like you pulled that quote from the comp.lang.c FAQ (maybe an old version or maybe the printed version; it doesn't quite match with the current state of the online one):

看来你是从comp.lang中摘录的。c FAQ(可能是旧版本,也可能是打印版本;它并不完全符合当前在线的状态:

http://c-faq.com/aryptr/aryptrequiv.html

http://c-faq.com/aryptr/aryptrequiv.html

The corresponding section links to other sections of the FAQ to elaborate on those exceptions. In your case, you should look at:

相应的部分链接到FAQ的其他部分来详细说明这些异常。在你的情况下,你应该看看:

http://c-faq.com/decl/strlitinit.html

http://c-faq.com/decl/strlitinit.html