How to pass various structures to function?

F

Fred

Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:


myfunc(char * str, struct astruct *as)


From googling, it always appears that one specific
structure is passed to the function.

-TIA
 
D

Dave Vandervies

Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function?

What are you really trying to do?

I'm having trouble coming up with a case where the Right Answer is
something other than "write a different function for each struct",
possibly in the degenerate case of "use a single struct for all of
the options".


dave
 
M

Martin Ambuhl

Fred said:
Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:


myfunc(char * str, struct astruct *as)


From googling, it always appears that one specific
structure is passed to the function.

Use the facilities of <stdarg.h>. Remember that you need at least one
named argument after which the variable argument list occurs. In your
case the poorly-named char *str could be used to pass information about
the number and types of arguments, as does the format string for the
printf() and scanf() families. Your example uses pointers-to-structs
which is a good idea if you are mixing differently defined struct
arguments as well as changing their number.

I would, however, suggest that you start by learning how to use variable
argument lists with the arguments being drawn from the fundamental
integer and floating types before moving on to more complex arguments
and argument lists.
 
F

Fred

What are you really trying to do?

I'm having trouble coming up with a case where the Right Answer is
something other than "write a different function for each struct",
possibly in the degenerate case of "use a single struct for all of
the options".


dave



I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.
 
C

Chris Johnson

Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:

myfunc(char * str, struct astruct *as)

From googling, it always appears that one specific
structure is passed to the function.

-TIA

A void * argument will accept a pointer to any data type. However, it
could just as easily be passed a char[] or an int as a struct, so if
you want to catch those possibilities, this wouldn't work for you. And
there's no way to retrieve the original type directly -- it could be
done with an extra parameter and selection logic.
 
E

Eric Sosman

Fred said:
I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.

If the function doesn't know what the struct looks
like, how will it know where to search?

For example, let's say you have two structs

struct a { int whiz; char *name; };
struct b { char *this, *that; double whiz; }

.... and pretend there's a magical way to get the function
to accept either of them[*]:

int myfunc(const *key, struct a_or_b *sptr) {
...
}

Knowing only that its second argument points to a struct a
or to a struct b, but not knowing which, what exactly are
you planning to have myfunc() do?

[*] Actually, it's not so magical: There's a way to
pass a "pointer to anything at all" to a function, by using
a function parameter of type `void*'. But this leaves you
in exactly the same hole: You know that the argument points
at something, but you don't know what kind of a something
it points at. Unless you can deduce the nature of the
pointed-at thing from some other piece of information, the
things you can do with an "opaque" type are fairly limited.
 
R

Richard Heathfield

Fred said:
I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.

How will the function know what kind of structure you're passing it? It
will need this information, in order to know how to conduct the search.
 
F

Flash Gordon

Richard Heathfield wrote, On 03/05/07 05:33:
Fred said:


How will the function know what kind of structure you're passing it? It
will need this information, in order to know how to conduct the search.

Unless the key to be searched on is a common initial sequence to all the
structure types of interest.

Without knowing more we can't say what the best solution is, that much
is certain.
 
N

Nick Keighley

I'm trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.

how will your function know where to look for the data?

What do the structs look like? What does the data look like?
 
X

Xavier Serrand

Fred said:
Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:


myfunc(char * str, struct astruct *as)


From googling, it always appears that one specific
structure is passed to the function.

-TIA

Such a polymorphism is available in c : lool at stdarg.h
I suppose you will use the first argumeny as a type identifier?...

Xavier
 
K

kingfox

Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:
I think you can do so:

void function(char *str, void *ptrList[])
{
/*
you can scan the content in str and determine which element in
ptrList should be used.
*/
}

/* usage of function: */
struct astruct as;
struct bstruct bs;
struct cstruct cs;
/* ... and so on */

void *ptrList[] = {&as, &bs, &cs/*, ... and so on */};
function(str, ptrList);

Sorry for my poor English. Hope my answer would give you some help.
 
J

Joe Estock

Eric said:
If the function doesn't know what the struct looks
like, how will it know where to search?

For example, let's say you have two structs

#define TYPE_A 1
#define TYPE_B 2
....
#define TYPE_Z 26
struct a { int whiz; char *name; };
struct b { char *this, *that; double whiz; }

struct a { int type; int whiz; char *name; };
struct b { int type; char *this, *that; double whiz; };
....
struct z { int type; float p; char *bar; int foo; };
... and pretend there's a magical way to get the function
to accept either of them[*]:

int myfunc(const *key, struct a_or_b *sptr) {
...
}

int myfunc(void *key, void *sptr)
{
switch (sptr->type)
{
case TYPE_A:
/* logic here */
break;
...
}

...
}

Be forewarned, however, that I'm not sure if "sptr->type" is legal. It
may be better to pass the structure type as an additional parameter
instead of making it a member of each structure. The function would be
(obviously) huge depending on the number of structure's however it may
be more maintainable than several functions which do (mostly) the same
thing. Again this all depends on the implementation and personally I
would prefer the different function approach whenever possible.
Knowing only that its second argument points to a struct a
or to a struct b, but not knowing which, what exactly are
you planning to have myfunc() do?

[*] Actually, it's not so magical: There's a way to
pass a "pointer to anything at all" to a function, by using
a function parameter of type `void*'. But this leaves you
in exactly the same hole: You know that the argument points
at something, but you don't know what kind of a something
it points at. Unless you can deduce the nature of the
pointed-at thing from some other piece of information, the
things you can do with an "opaque" type are fairly limited.
 
J

Jack Klein

#define TYPE_A 1
#define TYPE_B 2
...
#define TYPE_Z 26

Ugh, no, that's hideous and hard to maintain. Instead:

enum data_type {
TYPE_A,
TYPE_B,
/* etc. */
TYPE_Z
};
struct a { int type; int whiz; char *name; };
struct b { int type; char *this, *that; double whiz; };
...
struct z { int type; float p; char *bar; int foo; };

No, let's keep it legal, defined C. Do this with Eric's structure
definitions:

struct data
{
enum data_type type;
union
{
struct a a;
struct b b;
/* etc. */
}
};

... and pretend there's a magical way to get the function
to accept either of them[*]:

int myfunc(const *key, struct a_or_b *sptr) {
...
}

int myfunc(void *key, void *sptr)
{
switch (sptr->type)
{
case TYPE_A:
/* logic here */
break;
...
}

...
}

Be forewarned, however, that I'm not sure if "sptr->type" is legal. It

No, it's not. But with the union approach:

int myfunc(struct data *data)
{
switch (data->type)
{
case TYPE_A:
/* yada, yada, yada */
may be better to pass the structure type as an additional parameter
instead of making it a member of each structure. The function would be
(obviously) huge depending on the number of structure's however it may
be more maintainable than several functions which do (mostly) the same
thing. Again this all depends on the implementation and personally I
would prefer the different function approach whenever possible.
Knowing only that its second argument points to a struct a
or to a struct b, but not knowing which, what exactly are
you planning to have myfunc() do?

[*] Actually, it's not so magical: There's a way to
pass a "pointer to anything at all" to a function, by using
a function parameter of type `void*'. But this leaves you
in exactly the same hole: You know that the argument points
at something, but you don't know what kind of a something
it points at. Unless you can deduce the nature of the
pointed-at thing from some other piece of information, the
things you can do with an "opaque" type are fairly limited.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top