Passing two different structures to the same function

I have 2 different sizes of structure and I would like to have one function to which I can pass them. However, I do not know how to define a function argument to accept two different structures.

My structures below

struct { int a; // 2 byte int b; // 2 byte int c; // 2 byte int d; // 2 byte } person1; // 8 bytes struct { int a; // 2 byte DeviceAddress b; // 8 bytes int c // 2 bytes float d; // 4 bytes } person2; // 16 bytes function print_struct(struct& ?????) { actions here.... } print_struct(person1); print_struct(person2); 
+4
source share
4 answers

Unfortunately, the only choice for unrelated structures in C is to pass pointers to untyped structures (that is, like void* ) and pass the type to the side, for example:

 struct person1_t { int a; // 2 byte int b; // 2 byte int c; // 2 byte int d; // 2 byte } person1; struct person2_t { int a; // 2 byte DeviceAddress b; // 8 bytes int c // 2 bytes float d; // 4 bytes } person2; void print_struct(void* ptr, int structKind) { switch (structKind) { case 1: struct person1 *p1 = (struct person1_t*)ptr; // Print p1->a, p1->b, and so on break; case 2: struct person2 *p2 = (struct person2_t*)ptr; // Print p2->a, p2->b, and so on break; } } print_struct(&person1, 1); print_struct(&person2, 2); 

This approach is highly error prone since the compiler cannot perform type checking for you.

+4
source

This is not real. You can create a union that contains two structures plus some identifier. Then you pass the union and use the identifier to determine what structure it contains.

 typedef struct sp1 { int a; // 2 byte int b; // 2 byte int c; // 2 byte int d; // 2 byte } person1_t; // 8 bytes typedef struct sp2 { int a; // 2 byte DeviceAddress b; // 8 bytes int c // 2 bytes float d; // 4 bytes } person2_t; // 16 bytes typedef union { person1_t person1; person2_t person2; } people; function print_struct(people *p, int id) // eg id == 1, struct is person1 { switch (id) { case 1: // Do person 1 things break; case 2: // Do person 2 things break; default: // Error break; } } 
+2
source

As dasblinkenlight said, if you want to be able to pass two different structures to functions, using void * to pass a shared pointer would be correct, however, it is not type safe and could easily lead to erroneous code.

What functionality are you trying to achieve with two separate structures? Could you consider the possibility of combining information into one structure instead of it and the presence of a print function that displays all non-zero values?

Forgive me, perhaps the non-optimal c code, I am far from an expert, this is just to illustrate the point :)

 typedef struct datastruct { int a; int b; float c; } datastruct; void printData(datastruct *d){ printf("Data:\n") printf((d->a) ? "a=%d", a : ""); printf((d->b) ? "b=%d", b : ""); printf((d->c) ? "c=%.2f", c : ""); printf("\n"); } int main(void) { datastruct data = {0}; /* now set values as needed */ printData(&data); return 0; } 
0
source
 typedef struct p1 { int a; // 2 byte int b; // 2 byte int c; // 2 byte int d; // 2 byte } person1; // 8 bytes typedef struct p2{ int a; // 2 byte DeviceAddress b; // 8 bytes int c // 2 bytes float d; // 4 bytes } person2; // 16 bytes typedef enum ptypes { PERSON1, PERSON2 } person_type; typedef union p1_or_p2 { person1 p1; person2 p2; } person1_or_person2; typedef struct p { person1_or_person2 person; person_type type; } person; // Creating a person struct variable: person p; person1 p1; p1.a = 5; p1.b = 2; p.type = PERSON1; p.person = (person1_or_person2) p1; void print_struct(person p) { switch (p.type) { case PERSON1: // actions for person1 here.... // you can access person1 like this: p.person.p1; break; case PERSON2: // actions for person2 here.... // you can access person2 like this: p.person.p2; break; } } 
-2
source

All Articles