Search code examples
cif-statementstructconcatenationconditional-operator

How to compose a variable name in one line, as part of struct call?


I have NO idea if what i'm trying to accomplish is doable at all. I'm pretty new to C.

I have a struct that contains a pointer to another struct, that contains yet another pointer to another struct. The last struct contains two pointers to structs called "leadingToCw" and "leadingToCcW". I also have a global String variable dir which can change from Cw to Ccw at any point in time. It looks someting like this:

#define concat(a,b) a##b
String dir = "Ccw"; // or "Cw"

struct Intersection {
    struct Sector *incoming;
}

struct Sector {
    struct Sector *leadingToCcw;
    struct Sector *leadingToCw;
};

Now the thing i want to is call the leadingToCcw or leadingToCw depending on the "dir" string, so that the output would look like this:

Intersection->incoming->leadingToCcw

I've tried something like this:

Intersection->incoming->(dir == "Ccw" ? concat(leadingTo, Ccw) : concat(leadingTo, Cw))

but i get this error:

error: expected unqualified-id before '(' token

I don't know if it's worth mentioning but it is for an Arduino project :-) I hope someone can help me, or kick me in the butt and tell me that it is not feasible!


Solution

  • Why don't use a usual condition?

    dir == "Ccw"? Intersection->incoming->leadingToCcw : Intersection->incoming->leadingToCw