Robby
2009-08-29 17:12:01 UTC
Hello,
I figured reading up on the advanced uses of void pointer (void*) would shed
some light on what I am trying to do, but this is not working out. I read
stuff like:
http://pointersinc.blogspot.com/
but I don't see how function pointers could solve my problem.
It so happens that I have many functions that do exactly the same thing but
for different object types. I really would like to replace all these
functions by one function and alter the type of the pointer to the correct
object at function calling time (when my code actually calls the function).
For example here is what I have now:
=====================================
typedef struct tag_pc {
long LK__F1;
} pc;
typedef struct tag_ab {
long LK__F1;
} ab;
typedef struct tag_cd {
long LK__F1;
} cd;
void f1(pc *obj_pc)
{
obj_pc->LK__F1 = 10;
}
void f2(ab *obj_ab)
{
obj_ab->LK__F1 = 10;
}
void f3(cd *obj_cd)
{
obj_cd->LK__F1 = 10;
}
int main()
{
pc *obj_pc;
ab *obj_ab;
cd *obj_cd;
obj_pc = malloc (sizeof (struct tag_pc));
obj_ab = malloc (sizeof (struct tag_ab));
obj_cd = malloc (sizeof (struct tag_cd));
f1(obj_pc);
f2(obj_ab);
f3(obj_cd);
free(obj_pc);
free(obj_ab);
free(obj_cd);
return 0;
}
============================
Keeping f1(), f2() and f3 around while they carry out the exact same logic
functionality is absurb. Here is what I am trying to do:
============================
typedef struct tag_pc {
long LK__F1;
} pc;
typedef struct tag_ab {
long LK__F1;
} ab;
typedef struct tag_cd {
long LK__F1;
} cd;
void f1(void *x, int type)
{
switch(type)
{
case 1: pc *p = x; break;
case 2: ab *p = x; break;
case 2: cd *p = x; break;
}
p->LK__F1 = 10;
}
int main()
{
void *x;
x = malloc (sizeof (struct tag_pc));
f1(x, 1);
x = malloc (sizeof (struct tag_ab));
f1(x, 2);
x = malloc (sizeof (struct tag_cd));
f1(x, 3);
free(x);
return 0;
}
===========================
I get a multitude of errors at the function:
void f1(void *x, int type)
{
switch(type)
{
case 1: pc *p = x; break; <<<< error here!!!!
case 2: ab *p = x; break;
case 2: cd *p = x; break;
}
p->LK__F1 = 10;
}
I would have to do this for it to compile without errors:
void f1(void *x)
{
pc *p = x;
p->LK__F1 = 10;
}
*but then* How do I know what pointer *type* is comming in the function ???
Any ideas that would help condense my repetitive functions into one would
really make my code tighter and easier for me to follow in the future.
All help appreciated and thankyou in advance!
Finest regards
Roberto
I figured reading up on the advanced uses of void pointer (void*) would shed
some light on what I am trying to do, but this is not working out. I read
stuff like:
http://pointersinc.blogspot.com/
but I don't see how function pointers could solve my problem.
It so happens that I have many functions that do exactly the same thing but
for different object types. I really would like to replace all these
functions by one function and alter the type of the pointer to the correct
object at function calling time (when my code actually calls the function).
For example here is what I have now:
=====================================
typedef struct tag_pc {
long LK__F1;
} pc;
typedef struct tag_ab {
long LK__F1;
} ab;
typedef struct tag_cd {
long LK__F1;
} cd;
void f1(pc *obj_pc)
{
obj_pc->LK__F1 = 10;
}
void f2(ab *obj_ab)
{
obj_ab->LK__F1 = 10;
}
void f3(cd *obj_cd)
{
obj_cd->LK__F1 = 10;
}
int main()
{
pc *obj_pc;
ab *obj_ab;
cd *obj_cd;
obj_pc = malloc (sizeof (struct tag_pc));
obj_ab = malloc (sizeof (struct tag_ab));
obj_cd = malloc (sizeof (struct tag_cd));
f1(obj_pc);
f2(obj_ab);
f3(obj_cd);
free(obj_pc);
free(obj_ab);
free(obj_cd);
return 0;
}
============================
Keeping f1(), f2() and f3 around while they carry out the exact same logic
functionality is absurb. Here is what I am trying to do:
============================
typedef struct tag_pc {
long LK__F1;
} pc;
typedef struct tag_ab {
long LK__F1;
} ab;
typedef struct tag_cd {
long LK__F1;
} cd;
void f1(void *x, int type)
{
switch(type)
{
case 1: pc *p = x; break;
case 2: ab *p = x; break;
case 2: cd *p = x; break;
}
p->LK__F1 = 10;
}
int main()
{
void *x;
x = malloc (sizeof (struct tag_pc));
f1(x, 1);
x = malloc (sizeof (struct tag_ab));
f1(x, 2);
x = malloc (sizeof (struct tag_cd));
f1(x, 3);
free(x);
return 0;
}
===========================
I get a multitude of errors at the function:
void f1(void *x, int type)
{
switch(type)
{
case 1: pc *p = x; break; <<<< error here!!!!
case 2: ab *p = x; break;
case 2: cd *p = x; break;
}
p->LK__F1 = 10;
}
I would have to do this for it to compile without errors:
void f1(void *x)
{
pc *p = x;
p->LK__F1 = 10;
}
*but then* How do I know what pointer *type* is comming in the function ???
Any ideas that would help condense my repetitive functions into one would
really make my code tighter and easier for me to follow in the future.
All help appreciated and thankyou in advance!
Finest regards
Roberto