简体   繁体   English

指定表达式右侧的数组类型

[英]Specifying type of array on right side of expression

With the following:具有以下内容:

typedef struct Person_ {
    char* name;
    char* parents[2];
} Person;

int main(void)
{
    Person jack = {.name="Jack", .parents={"Jim", "Julia"}};
    Person tom = {.name="Tom", .parents={"Terry", "Tina"}};
    Person friends[2] = (Person[2]) {jack, tom};
}

I get this error:我收到此错误:

error: array initialized from non-constant array expression错误:从非常量数组表达式初始化的数组
Person friends[2] = (Person[2]) {jack, tom};人朋友[2] = (人[2]) {杰克, 汤姆};

I know that I can initialize friends as:我知道我可以将friends初始化为:

Person friends[2] = {jack, tom};

But why isn't it possible to annotate the type on the right side with an array like I would be able to (though redundantly) with another type such as:但是为什么不能用数组来注释右侧的类型,就像我可以(尽管是多余的)用另一种类型来注释的,例如:

int x = (int) 4;
char *y = (char*) "Hi";

// invalid
// int z[2] = (int[2]) {1,2}; 

In this statement在这份声明中

Person friends[2] = (Person[2]) {jack, tom};

(Person[2]) {jack, tom} is a compound literal. (Person[2]) {jack, tom}是复合文字。

From C11 Standard - Compound literals#6.5.2.5p5来自 C11 标准 - 复合文字#6.5.2.5p5

5 The value of the compound literal is that of an unnamed object initialized by the initializer list. 5 复合文字的值是由初始化列表初始化的未命名 object 的值。 .... ……

So, this (Person[2]) {jack, tom} will result in an unnamed array of Person type initialised from the initialiser list {jack, tom} .所以,这个(Person[2]) {jack, tom}将产生一个从初始化列表{jack, tom}初始化的Person类型的未命名数组。 You cannot intialize an array from another array.您不能从另一个数组初始化一个数组。

From C11 Standard - Compound literals#6.7.9p16来自 C11 标准 - 复合文字#6.7.9p16

16 Otherwise, the initializer for an object that has aggregate or union type shall be a brace- enclosed list of initializers for the elements or named members. 16 否则,具有聚合或联合类型的 object 的初始化程序应为元素或命名成员的初始化程序的括号括起来的列表。

This {jack, tom} is initialiser list whereas this (Person[2]) {jack, tom} is compound literal.这个{jack, tom}是初始化列表,而这个(Person[2]) {jack, tom}是复合文字。

The compound literal results in an unnamed array, initialises it with given values and creates a pointer point to first element of array.复合文字产生一个未命名的数组,用给定的值对其进行初始化,并创建一个指向数组第一个元素的指针。 So, in this statement所以,在这个声明中

int *a1  = (int[2]) {1,2};

(int[2]) {1,2} will result in an unnamed array of 2 int initialised with value {1, 2} and create pointer to initial element of unnamed array. (int[2]) {1,2}将产生一个2 int的未命名数组,用值{1, 2}初始化,并创建指向未命名数组初始元素的指针。

From C11 Standard#6.3.2.1p3来自 C11 标准#6.3.2.1p3

3 Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 3 除非它是 sizeof 运算符、_Alignof 运算符或一元 & 运算符的操作数,或者是用于初始化数组的字符串字面量,否则类型为 ''array of type'' 的表达式将转换为表达式类型为“指向类型的指针”,它指向数组 object 的初始元素,并且不是左值。 .... ……

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

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