C11 嵌套初始化
C11 Nested Initialisation
在 C11 中,有没有办法创建嵌套初始化来实现我在 'test_successor_set' 中概述的内容?
Google 搜索嵌套的 C11 初始化到目前为止还没有显示任何示例。
也许这是不可能的。
我已经走到这一步了,但我无法让它工作。
typedef struct my_node_successor_struct {
char *node_name;
float cost;
} my_node_successor;
typedef struct my_node_successor_set_struct {
const char *successor_name;
my_node_successor *successors[];
} my_node_successor_set;
my_node_successor_set test_successor_set[] = {
{ .node_name = "S", .successors = { { .successor_name = "A", .cost = 2.f}, { .successor_name = "C", .cost = 3.f }, }},
{ .node_name = "A", .successors = { { .successor_name = "B", .cost = 1.f}, { .successor_name = "C", .cost = 1.f }, }},
{ .node_name = "C", .successors = { { .successor_name = "G", .cost = 3.f}, }},
{ .node_name = NULL, NULL },
};
没有
你得到了一个结构,它的末尾有一个灵活的数组成员(它唯一可以出现的地方)。但是你不能初始化这种结构的数组,也不能合法地创建这种结构的数组(因为数组的所有元素必须具有相同的大小,而 FAM 的要点是具有可变大小,在任何情况下,该数组都不计入大小,因此 FAM 数组中的元素只能为零)。
标准的相关部分,ISO/IEC9899:2011是
§6.7.2.1 Structure and union specifiers
¶3 A structure or union shall not contain a member with incomplete or function type (hence,
a structure shall not contain an instance of itself, but may contain a pointer to an instance
of itself), except that the last member of a structure with more than one named member
may have incomplete array type; such a structure (and any union containing, possibly
recursively, a member that is such a structure) shall not be a member of a structure or an
element of an array.
¶18 As a special case, the last element of a structure with more than one named member may
have an incomplete array type; this is called a flexible array member. In most situations,
the flexible array member is ignored. In particular, the size of the structure is as if the
flexible array member were omitted except that it may have more trailing padding than
the omission would imply. However, when a .
(or ->
) operator has a left operand that is
(a pointer to) a structure with a flexible array member and the right operand names that
member, it behaves as if that member were replaced with the longest array (with the same
element type) that would not make the structure larger than the object being accessed; the
offset of the array shall remain that of the flexible array member, even if this would differ
from that of the replacement array. If this array would have no elements, it behaves as if
it had one element but the behavior is undefined if any attempt is made to access that
element or to generate a pointer one past it.
在 C11 中,有没有办法创建嵌套初始化来实现我在 'test_successor_set' 中概述的内容?
Google 搜索嵌套的 C11 初始化到目前为止还没有显示任何示例。
也许这是不可能的。
我已经走到这一步了,但我无法让它工作。
typedef struct my_node_successor_struct {
char *node_name;
float cost;
} my_node_successor;
typedef struct my_node_successor_set_struct {
const char *successor_name;
my_node_successor *successors[];
} my_node_successor_set;
my_node_successor_set test_successor_set[] = {
{ .node_name = "S", .successors = { { .successor_name = "A", .cost = 2.f}, { .successor_name = "C", .cost = 3.f }, }},
{ .node_name = "A", .successors = { { .successor_name = "B", .cost = 1.f}, { .successor_name = "C", .cost = 1.f }, }},
{ .node_name = "C", .successors = { { .successor_name = "G", .cost = 3.f}, }},
{ .node_name = NULL, NULL },
};
没有
你得到了一个结构,它的末尾有一个灵活的数组成员(它唯一可以出现的地方)。但是你不能初始化这种结构的数组,也不能合法地创建这种结构的数组(因为数组的所有元素必须具有相同的大小,而 FAM 的要点是具有可变大小,在任何情况下,该数组都不计入大小,因此 FAM 数组中的元素只能为零)。
标准的相关部分,ISO/IEC9899:2011是
§6.7.2.1 Structure and union specifiers
¶3 A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.
¶18 As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a
.
(or->
) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.