0

I have the following struct:

struct type1 {
    struct type2 *node;
    union element {
        struct type3 *e;
        int val;
    };
};

When initialising a pointer *f that points to an instance of type1 and doing something like: f.element->e or even just f.element, I get:

error: request for member ‘element’ in something not a structure or union

What am I overseeing here?

7
  • Can you show the declaration of f? Maybe you mean f->element.e? Commented Oct 23, 2012 at 19:36
  • 2
    element is the union tag, not a member name. Try with struct { ... union element { ... } elem; }; and f.elem. Commented Oct 23, 2012 at 19:37
  • 2
    @Darksky Your struct member needs a name to access it. The name is given after the type. struct type1 { ... ; union { ... } element; }; would create a member element with type (untagged) union {...}. Commented Oct 23, 2012 at 19:41
  • 1
    @Darksky: In C 1999 and earlier, adding a name to the end of the union declaration does not just give it a different name than its type. It gives it a name. Without that, it does not have a name; there is no object, no member in the enclosing struct. Without that, it is just a declaration of a type, not of a member. Commented Oct 23, 2012 at 19:58
  • 1
    @DanielFischer: C 2011 introduced anonymous unions and structures, in which case the union does not need a name. Commented Oct 23, 2012 at 19:59

2 Answers 2

3

element is the name of the union, not the name of a member of type1. You must give union element a name:

struct type1 {
struct type2 *node;
    union element {
        struct type3 *e;
        int val;
    } x;
};

then you can access it as:

struct type1 *f;
f->x.e
Sign up to request clarification or add additional context in comments.

5 Comments

Maybe you should malloc memory for your f?
f must be initialized somehow, yes.
There may be some issue of C versions here. C 2011 introduced anonymous structures and unions, in which case f->e would work with the original declaration. However, if Darksky is using an earlier version of C, then the union declaration must be modified as shown in this answer.
@EricPostpischil Correct, but even then, one wouldn't say: f->element.e
f is already initialised in malloc. This solves it. Thanks.
-1

If f is a pointer, then you may access "element" using f->element, or (*f).element

Update: just saw that "element" is the union name, not a member of the struct. You may try

union element {
    struct type3 *e;
    int val;
} element;

So the final struct would be like this:

struct type1 {
    struct type2 *node;
    union element {
        struct type3 *e;
        int val;
    } element;
};

And now you can access element members like this, through a type1 *f:

struct type1 *f;

// assign f somewhere

f->element.val;

4 Comments

Read the question please. That's what I did and that's the error I am getting.
@Darksky No. You did f.element and not f->element acording to your question.
No I did do both. Quoting: f.element->e or even just f.element, I get.
@Darksky f.element->e or f.element is not the same as f->element. f. will always be an error with a pointer *f (but anyway the problem with the union still was there before the edit). Regards

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.