c - 如何区分union的内容

标签 c unions

我只是想知道,如果我有一个接受其中包含 union 的结构的函数,我如何知道该 union 的内容:

struct command{
  int *input;
  int *output;
  union{
    char **word;
    struct command *subcommand;
  } u;
 };

函数:

void readCommand(command cInput){
    if(cInput.u is char) print the content of array of array of char
    else readCommand(cInput.u); //make a recursive call
}

有什么建议吗?谢谢

注意:我无法更改struct 命令的内容。

最佳答案

你不能。这就是 union 的本质。如果需要,您必须将 union 嵌入到结构中,并在结构中放置类型指示符。

关于c - 如何区分union的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384588/

相关文章:

c++ - 未使用的变量和函数的链接器错误

c++ - 在 C+ +'s ` extern "C"` block 中使用 c99

c - GCC 内联汇编乘法

c - 在c中访问 union 成员

c - C90 和 C99 中复合类型对象的对齐

c - 如何将混合类型数组转换为 char 数组并返回?

C 函数参数值自行改变

c - 如何从文件中的不同行读取字符串和整数

c++ - 使用 std::launder 从指向非事件对象的指针获取指向事件对象成员的指针?

c - 通过地址访问 union 的实际成员