gpt4 book ai didi

c - 嵌套大括号和指定的初始化器

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:29 25 4
gpt4 key购买 nike

为什么我需要嵌套大括号来编译这段代码?

#include <stdio.h>
#include <stdlib.h>

typedef union TEST TEST;

union TEST {
int type;
char *things;
};


int main () {

TEST test[] = {1, 2, .things = NULL}; // Compile time error

TEST test[] = {1, 2, {.things = NULL}}; // Works, but I didn't check at run time

return 0;
}

为什么第一次赋值不成功?
为什么需要内括号?
有没有办法在第一个任务中工作?没有内括号?

谢谢!

最佳答案

前两个元素不需要内括号这一事实在初始化列表的语法中是一个奇怪的细节,它允许 union 被初始化为它的第一个元素。我建议对列表中的所有 初始值设定项使用大括号和名称,而不仅仅是 .things

关于c - 嵌套大括号和指定的初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21888797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com