C - 创建一个匿名结构实例
C - Creating an anonymous struct instance
在this code中定义了一个结构如下:
typedef struct
{
int line;
int column;
} Pos;
后来是这样用的:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
行 return ( Pos ){ f->line, f->column + delta }
似乎正在创建具有初始化值的结构 Pos
的匿名实例。这种技术叫什么,它是如何工作的?我在哪里可以了解更多信息?
这称为 复合文字,并记录在 C standard.
的第 6.5.2.5 节中
本节内容摘录如下:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a
compound literal. It provides an unnamed object whose value is
given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in
6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an
object type), the type of the compound literal is that specified by
the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound
literal occurs outside the body of a function, the object has
static storage duration; otherwise, it has automatic storage
duration associated with the enclosing block.
在您的例子中,复合文字用于 struct
,但它们也可以为数组创建。第8段举例:
8 EXAMPLE 1 The file scope definition
int *p = (int []){2, 4};
initializes p
to point to the first element of an array of
two ints, the first having the value two and the second,
four. The expressions in this compound literal are required
to be constant. The unnamed object has static storage duration.
另请注意,复合文字是左值,这意味着您可以获取其地址:
Pos *p = &( Pos ){ f->line, f->column + delta };
此对象的生命周期与其作用域相关联,这意味着一旦作用域结束,该对象将不再存在。所以不要在它超出范围后携带它的地址。
您还可以使用带有 指定初始值设定项的复合文字:
return ( Pos ){ .line=f->line, .column=f->column + delta };
在this code中定义了一个结构如下:
typedef struct
{
int line;
int column;
} Pos;
后来是这样用的:
Pos get_pos ( int delta )
{
...
return ( Pos ){ f->line, f->column + delta };
}
行 return ( Pos ){ f->line, f->column + delta }
似乎正在创建具有初始化值的结构 Pos
的匿名实例。这种技术叫什么,它是如何工作的?我在哪里可以了解更多信息?
这称为 复合文字,并记录在 C standard.
的第 6.5.2.5 节中本节内容摘录如下:
3 A postfix expression that consists of a parenthesized type name followed by a brace- enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.
4 If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an object type), the type of the compound literal is that specified by the type name. In either case, the result is an lvalue.
5 The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.
在您的例子中,复合文字用于 struct
,但它们也可以为数组创建。第8段举例:
8 EXAMPLE 1 The file scope definition
int *p = (int []){2, 4};
initializes
p
to point to the first element of an array of two ints, the first having the value two and the second, four. The expressions in this compound literal are required to be constant. The unnamed object has static storage duration.
另请注意,复合文字是左值,这意味着您可以获取其地址:
Pos *p = &( Pos ){ f->line, f->column + delta };
此对象的生命周期与其作用域相关联,这意味着一旦作用域结束,该对象将不再存在。所以不要在它超出范围后携带它的地址。
您还可以使用带有 指定初始值设定项的复合文字:
return ( Pos ){ .line=f->line, .column=f->column + delta };