1

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?

3 Answers 3

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;

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
2

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.

Comments

0

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.

1 Comment

There is nothing in the code snippet or error suggesting a missing header. All members of the structs and unions are built-in C types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.