Yes, you can use a prototype, for example:
void foo(char type, void *data);
Use this type to tell the function which structure uses the data like, and you are good.
struct c *prepareStructC(void); //... struct c *toto = prepareStructC(); foo('c', toto); //... void foo(char type, void *data) { int x, y; switch (type) { case 'c': x = ((struct c*)data)->x; y = ((struct c*)data)->y; break; //... } //... }
The second option, if you want to avoid the / case switch, and after that you can add more structure types without changing foo, you can make sure that all your structures start with the necessary data, always in the same order, with the same type. This way you can do something like an โinterfaceโ with C ++ and use abstract versions of the type:
struct abstract { int x; int y; int radius; } struct a { struct abstract abs;
The second part of the second method allows you more flexibility by breaking down certain information in a subtype, allows you to place the abs part anywhere in the a / b / c structure and is better in principle. The only purpose for the structure (having coordinates and radius and other things inside the structure is not always the best .)
Eregrith
source share