views:

543

answers:

3

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?

A: 

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.

Joey Adams
There is nothing in the code snippet or error suggesting a missing header. All members of the `struct`s and `union`s are built-in C types.
dreamlax
+2  A: 

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.

quinmars
+9  A: 

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.

dreamlax
You're exactly right, the c compiler for iphone does not allow anonymous unions/structs. i fixed that and it now works properly! thank you for your help!
ilustreous
Now, come on, mark it as the answer.
mitjak