Y
yugidnu
Hi all,
I have the following problem.
I have to access different types of structures getting passed via a
void pointer.
I have the code snippet here.
---------------------------------------------------------------------------------------------------------------------
/* N structures each having different type of member variable .*/
/* But each structure has only one member */
typedef struct {
type1_t *member_var;
}st1;
typedef struct {
type2_t *member_var;
}st2;
typedef struct {
type3_t *member_var;
}st3;
......
......
typedef struct {
typeN_t *member_var;
}stN;
/****************************************************************/
void func1()
{
....
func2(x,&stx); /* where x is anywhere between 1 to N */
....
}
void func2(int typ,void *var)
{
/* In this function we have to access the structure member */
/* The typ passed to this function indicates what is the structure
passed */
st1 *s1; st2 *s2; st3 *s3 ..... stN *sn; /* pointers to all types of
structures */
switch (typ)
{
case 1:
s1 = (st1 *) var;
/*then access the member like this s1->member_var*/
break;
case 2:
s2 = (st2 *) var;
/*then access the member like this s2->member_var*/
break;
....
....
case N:
sn = (st2 *) var;
/*then access the member like this sn->member_var*/
break;
default:
/* print some error */
}
}
---------------------------------------------------------------------------------------------------------
My problem is the func2 is getting ugly.
Is there a better way to do access the structure member without the
switch cases ?
Is there a way which can be used to access the members without having
to define pointers to all the types of structures ?
Thanks for your time,
Yugi
I have the following problem.
I have to access different types of structures getting passed via a
void pointer.
I have the code snippet here.
---------------------------------------------------------------------------------------------------------------------
/* N structures each having different type of member variable .*/
/* But each structure has only one member */
typedef struct {
type1_t *member_var;
}st1;
typedef struct {
type2_t *member_var;
}st2;
typedef struct {
type3_t *member_var;
}st3;
......
......
typedef struct {
typeN_t *member_var;
}stN;
/****************************************************************/
void func1()
{
....
func2(x,&stx); /* where x is anywhere between 1 to N */
....
}
void func2(int typ,void *var)
{
/* In this function we have to access the structure member */
/* The typ passed to this function indicates what is the structure
passed */
st1 *s1; st2 *s2; st3 *s3 ..... stN *sn; /* pointers to all types of
structures */
switch (typ)
{
case 1:
s1 = (st1 *) var;
/*then access the member like this s1->member_var*/
break;
case 2:
s2 = (st2 *) var;
/*then access the member like this s2->member_var*/
break;
....
....
case N:
sn = (st2 *) var;
/*then access the member like this sn->member_var*/
break;
default:
/* print some error */
}
}
---------------------------------------------------------------------------------------------------------
My problem is the func2 is getting ugly.
Is there a better way to do access the structure member without the
switch cases ?
Is there a way which can be used to access the members without having
to define pointers to all the types of structures ?
Thanks for your time,
Yugi