在目标c中使用c库

ilu*_*ous 1 c iphone objective-c

我在目标c中创建这个c结构时遇到了麻烦.

typedef struct huffman_node_tag
{
    unsigned char isLeaf;
    unsigned long count;
    struct huffman_node_tag *parent;

    union 
    {
        struct 
        {
            struct huffman_node_tag *zero, *one;
        };
        unsigned char symbol;
    };
} huffman_node;
Run Code Online (Sandbox Code Playgroud)

我在union类型的结尾处获得此警告,并且在"unsigned char symbol variable"之上的结构类型的末尾

警告:声明不会声明任何内容

然后,当我做这样的事情:

huffman_node *p = (huffman_node*)malloc(sizeof(huffman_node)); 
p->zero = zero; 
Run Code Online (Sandbox Code Playgroud)

我得到这个编译错误:

错误:'huffman_node'没有名为'zero'的成员

为什么这不起作用?我错误地设置了吗?有谁之前经历过这个吗?

dre*_*lax 15


typedef struct huffman_node_tag
{
    unsigned char isLeaf;
    unsigned long count;
    struct huffman_node_tag *parent;

    union 
    {
        struct 
        {
            struct huffman_node_tag *zero, *one;
        };    // problematic here!
        unsigned char symbol;
    }; // another problem here!
} huffman_node;
Run Code Online (Sandbox Code Playgroud)

根据对C方言/正被用来解释代码的编译器,你可能不会被允许申报struct或者union没有名字.尝试给他们起名字,看看会发生什么.或者,您可能想尝试更改正在使用的C方言.