1

Given the following struct,

typedef struct tCard {
    CardClass class;
    void *proto;
} Card;

typedef struct tCardPath {
    PathType path_type;
    struct tPath path;
    Goal goal;
} CardPath;

Is it possible to access the element pointed by a pointer to struct (proto) using macros, like this?

((CardPath*)(trial[i].proto))->element1; // this works
CARD_PROP(trial[i], Path, element1); // the goal

I tried this, but this gives error: expected identifier before ‘(’ token when compiling,

#define PROTO(C) (C).proto
#define CARD_PROP(C, CARD, PROP) (((Card##CARD *)(PROTO(C)))->(PROP))

EDIT: Tried this, still doesn't work

#define CARD_PROP(C, CARD, PROP) ((Card##CARD *)(PROTO(C))->PROP
2
  • Your new edited macro produces exactly ((CardPath *)((trial[i]).proto)->element1;. Notice the 4 ( and the 3 ) Commented Nov 14, 2013 at 12:11
  • May I ask why do this? Smells like an XY problem. meta.stackexchange.com/a/66378 Commented Nov 14, 2013 at 13:36

1 Answer 1

3

The problem is that you can't put members of a struct in parentheses. Your macro expands to:

((CardPath*)(trial[i].proto))->(element1)
                               ^^^^^^^^^^

Which shouldn't have parentheses where I marked above.

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

Comments

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.