 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Fred Guest
|
Posted: Thu May 03, 2007 8:04 am Post subject: How to pass various structures to function? |
|
|
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 |
|
| Back to top |
|
 |
Dave Vandervies Guest
|
Posted: Thu May 03, 2007 8:04 am Post subject: Re: How to pass various structures to function? |
|
|
In article <rv2dnaKFYbbIzKTbnZ2dnUVZ_uuqnZ2d (AT) comcast (DOT) com>,
Fred <itfred (AT) cdw (DOT) com> wrote:
| Quote: | 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
--
Dave Vandervies dj3vande (AT) csclub (DOT) uwaterloo.ca
There was a twit named Ross St John, who everyone wished would be gone.
He called himself Cass, We plonked him en-masse,
And let out a c-l-c yawn. --Dann Corbit in comp.lang.c |
|
| Back to top |
|
 |
Martin Ambuhl Guest
|
Posted: Thu May 03, 2007 8:12 am Post subject: Re: How to pass various structures to function? |
|
|
Fred wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Fred Guest
|
Posted: Thu May 03, 2007 8:31 am Post subject: Re: How to pass various structures to function? |
|
|
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Chris Johnson Guest
|
Posted: Thu May 03, 2007 8:56 am Post subject: Re: How to pass various structures to function? |
|
|
On May 2, 10:04 pm, Fred <itf...@cdw.com> wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Thu May 03, 2007 9:11 am Post subject: Re: How to pass various structures to function? |
|
|
Fred wrote:
| Quote: | On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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".
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.
--
Eric Sosman
esosman@acm-dot-org.invalid |
|
| Back to top |
|
 |
Flash Gordon Guest
|
Posted: Thu May 03, 2007 9:11 am Post subject: Re: How to pass various structures to function? |
|
|
Richard Heathfield wrote, On 03/05/07 05:33:
| Quote: | Fred said:
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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".
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.
|
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.
--
Flash Gordon |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Thu May 03, 2007 9:11 am Post subject: Re: How to pass various structures to function? |
|
|
Fred said:
| Quote: | On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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".
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.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Nick Keighley Guest
|
Posted: Thu May 03, 2007 9:11 am Post subject: Re: How to pass various structures to function? |
|
|
On 3 May, 04:31, Fred <itf...@cdw.com> wrote:
| Quote: | On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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.
|
how will your function know where to look for the data?
What do the structs look like? What does the data look like?
--
Nick Keighley |
|
| Back to top |
|
 |
Joe Estock Guest
|
Posted: Fri May 04, 2007 5:39 am Post subject: Re: How to pass various structures to function? |
|
|
Eric Sosman wrote:
| Quote: | Fred wrote:
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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".
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
|
#define TYPE_A 1
#define TYPE_B 2
....
#define TYPE_Z 26
| Quote: |
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; };
| Quote: |
... 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.
| Quote: |
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.
|
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Fri May 04, 2007 7:32 am Post subject: Re: How to pass various structures to function? |
|
|
On Thu, 03 May 2007 19:39:51 -0500, Joe Estock
<jestock (AT) NOSPAMnutextonline (DOT) com> wrote in comp.lang.c:
| Quote: | Eric Sosman wrote:
Fred wrote:
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:
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".
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
#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
};
| Quote: | 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; };
|
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. */
}
};
| Quote: | ... 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 */
| Quote: | 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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|