Accessing different structures via pointer

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
 
M

Mark Bluemel

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.

Seems a little pointless... Why have a structure with 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 */

You don't need these..
switch (typ)
{
case 1:
s1 = (st1 *) var;
/*then access the member like this s1->member_var*/
break;

or access via ((st1 *)var)->member_var ....

etc...
My problem is the func2 is getting ugly.

That's because your data structure design is ugly, I think...
Is there a better way to do access the structure member without the
switch cases ?

Not with this design for your data. There may be something you could do
with a union approach, but it would still need a switch.

In a real-world situation, I think I'd look at a different data structure.
Is there a way which can be used to access the members without having
to define pointers to all the types of structures ?

I've shown that above.
 
Y

yugidnu

Hi Mark,

Thanks for your reply.
> Seems a little pointless... Why have a structure with only one
member?

Yes i too agree.
But those structures and func1 belong to legacy code.I cannot change
that.

All i have to do is write func2.

Is there a better way to write func2. (avoiding switch cases) ?

Thanks for your time,
Yugi.
 
M

Mark Bluemel

Hi Mark,

Thanks for your reply.

member?

Yes i too agree.
But those structures and func1 belong to legacy code.I cannot change
that.

All i have to do is write func2.

Is there a better way to write func2. (avoiding switch cases) ?

It doesn't seem so. You can use casts to avoid creating all those
pointers, but otherwise you are restricted by your data structures.
 
B

Ben Bacarisse

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 */

The first parameter, x, is probably a classic case of passing
"control" rather than data into a function -- x is not needed to
perform the operation but to decide *what* calculation to
perform.
....

}



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 */

}


If, after collecting the data from the variously typed pointers, there
was a significant amount of common code here, then there would be a
case for writing it the way you have, but since there is none (at
least in your cut-down example) what you really have is N function all
written in one.

A better structure is to take the code from each case and make it a
function on its own:

void *f_st1(void *p)
{
st1 *stp = p;
/* use stp */
}

and so on... Now, your integer variable x becomes a function pointer
(the type is hairy, but can be simplified with typedefs):

void *(*x)(void *);

(I've kept the name, but you'd choose a better one!) and the
"dispatch" code 'func2(x, &stx)' becomes

void *result = x(&stx); /* your example discards the result */

Of course, whether this is useful depends on how your real program
actually uses func2. You don't show that, so it may be that this does
not help, but it is a standard pattern that is useful to know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top