473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing different structures via pointer

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

Nov 13 '07 #1
4 1945
yu*****@googlemail.com wrote:
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 */
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.
Nov 13 '07 #2
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.

Nov 13 '07 #3
yu*****@googlemail.com wrote:
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) ?
It doesn't seem so. You can use casts to avoid creating all those
pointers, but otherwise you are restricted by your data structures.
Nov 13 '07 #4
yu*****@googlemail.com writes:
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.

--
Ben.
Nov 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Xavier Noria | last post by:
Given two structures of the same size but different type, does C99 guarantee that pointers to them can be casted one to each other, and that the order of the elements will be kind of respected? ...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
7
by: =?Utf-8?B?U3RlcGhhbmllIERvaGVydHk=?= | last post by:
I am trying to call some API functions written in C++ from my VB .NET 2003 application and am running into some difficulty in passing structures to a function. It is probably due to the types I'm...
4
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.