Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is it possible to pass different structures to the one function?

Status
Not open for further replies.

Flappy

Programmer
Jul 30, 2002
35
AU
ive got a general function that will act apon whatever structure i want to pass to it but is it possible to do that?

i tried this:

void test(void* obj) {
obj->member=....
}

the compiler seems to allow the void* obj, but the compiler is giving errors when i try to access members of that function

 
That's 'cuz it's a void*, not a pointer to an object of the type you think you're operating on.

If you have:

Code:
struct X
{
    int num;
};

Then you can use a cast to turn the void* into an X* and operate on it:

Code:
void test( void* obj )
{
    ((X*)obj)->num = 7;
}


However, that's pretty futile, since you apparently want it to handle objects of different types, and the cast means you have to have knowledge of that type when you write the function, and if you did, you wouldn't have need of the void* and the cast anyway since you could just pass the proper type in the first place.

Unless the cast somehow handles your problem, methinks you want either a template function or a class heirarchy with virtual functions.
 
hmm ok - well its starting to look like it cant be done...

i cant see any point in the casting cause as you were saying if you know what you are casting it as then you may as well assign the proper type in the first place.. i think for the time being i'll just add in a switch statement and depending on the passed value will make the function change the obj whatever struct pointer it needs to be...

thanks
 
You can avoid adding an extra parameter to your function by including a type field as the first member of each different type of structure that can be passed to the function. e.g.,

enum obj_type {
OBJ_TYPE_FOO, OBJ_TYPE_BAR, OBJ_TYPE_BAZ
};

struct foo { enum obj_type type; char *member; };
struct bar { enum obj_type type; int member; };
struct baz { enum obj_type type; double member; };

Then in your generic function:

void test(void* obj) {
switch (*((enum obj_type *)obj)->type)) {
case OBJ_TYPE_FOO:
{
struct foo *o = obj;
printf("%s\n", o->member);
}
break;
case OBJ_TYPE_BAR:
{
struct bar *o = obj;
printf("%d\n", o->member);
}
break;
case OBJ_TYPE_BAZ:
{
struct baz *o = obj;
printf("%f\n", o->member);
}
break;
}
}

Since a pointer to a structure is guaranteed to point to its first member, this is a portable technique.
 
This problem seems to have 'union' written all over it

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum obj_type {
   OBJ_TYPE_FOO, OBJ_TYPE_BAR, OBJ_TYPE_BAZ
};

struct obj {
    enum obj_type type;
    union {
        char   *foo_member;
        int     bar_member;
        double  baz_member;
    } v;
};

void test ( struct obj* obj ) {
   switch ( obj->type ) {
      case OBJ_TYPE_FOO:
         printf(&quot;%s\n&quot;, obj->v.foo_member);
         break;
      case OBJ_TYPE_BAR:
         printf(&quot;%d\n&quot;, obj->v.bar_member);
         break;
      case OBJ_TYPE_BAZ:
         printf(&quot;%f\n&quot;, obj->v.baz_member);
         break;
   }  
}

int main() {
    struct obj  t1;
    t1.type = OBJ_TYPE_FOO;
    t1.v.foo_member = &quot;hello world&quot;;
    test( &t1 );
    system(&quot;pause&quot;);
    return 0;
}

And not a cast to be seen :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top