6 years, 2 months ago.

How to parse variables from a void to a function


Can anyone give me a hint on how this is done

posted by Hut The Nut 08 Mar 2016

Can you show with some (pseudo) code what exactly you try to do?

posted by Erik - 09 Mar 2016

1 Answer

6 years, 2 months ago.

Your question is vague but I'm going to take a guess at what you're asking ...

typedef struct {
    int x;
    int y;
    int z;
} thing1;

typedef struct {
    float scale;
    float offset;    
} thing2;

thing1 first = {1, 2, 3};
thing2 sec = {3.15f, -1.00f};

void myFunc(int thingType, void * pThing) {
    if (thingType == 1) {
        thing1 * ptr = (thing1 *)pThing;    // the cast lets us extract meaning behind the void *
        printf("thing1 has (%d,%d,%d)\r\n", ptr->x, ptr->y, ptr->z);
    } else if (thingType == 2) {
        thing2 * ptr = (thing2 *)pThing;
        printf("thing2 has (%f,%f)\r\n", ptr->scale, ptr->offset);
    } else {
        printf("unknown thingtype\r\n");
    }
}

... 
    myFunc(1, (void *)&first);
...
    myFunc(2, (void *)&sec);
...


Accepted Answer