开发者

Using c library in objective c

I'm having trouble creating this c struct in objective 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;

I'm getting this warning at the end of the union type and the end of the struct type above the "unsigned char symbol variable"

warning:开发者_运维知识库 declaration does not declare anything

And then when i do something like this:

huffman_node *p = (huffman_node*)malloc(sizeof(huffman_node)); 
p->zero = zero; 

I get this compilation error:

error: 'huffman_node' has no member named 'zero'

Why does this not work? Did i set this up incorrectly? Has anyone experienced this before?



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;

Depending on the C dialect/compiler that is being used to interpret the code, you may not be allowed to declare a struct or union without a name. Try giving them names and see what happens. Alternatively, you may want to try and change the C dialect you are using.


As far as I know anonymous unions are not part of C, but are a compiler extension. So strictly your given struct definition is not valid C. Consequently it seems that objective C does not supports this extension.


You need to include the header for the C library you are using.

You shouldn't have to do much else than that, as Objective C, unlike C++, is a strict superset of C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜