Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
声明一个结构并定义包含在 {} 中的值是否有效?
struct name new_name[] { {"value1"}, {"value2"}, {"value3"}, }
在哪里:
struct name { union { char *value1; } n_u; char *value2; }
您发布的内容无效,因为它在初始化程序(以及尾随分号)之前缺少等号。否则,它是合法的,但有点难以阅读,因为它没有初始化每个字段,也没有使用完整的大括号。在完全支撑的初始化程序中,每个数组、结构或联合的值列表周围都有一对花括号。在这种情况下,您有一个带有联合的结构数组,因此应该有 3 级大括号以获得最佳可读性。与所有内容的等价物是:
struct name new_name[] = { {{"value1"}, NULL}, {{"value2"}, NULL}, {{"value3"}, NULL}, };