473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
May 3 '07 #1
12 3822
Fred wrote:
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.
May 3 '07 #2
In article <rv******************************@comcast.com>,
Fred <it****@cdw.comwrote:
>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 dj******@csclub.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
May 3 '07 #3
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.

May 3 '07 #4
On May 2, 10:04 pm, Fred <itf...@cdw.comwrote:
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.

May 3 '07 #5
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

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
es*****@acm-dot-org.invalid

May 3 '07 #6
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 3 '07 #7
Richard Heathfield wrote, On 03/05/07 05:33:
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
May 3 '07 #8
On 3 May, 04:31, Fred <itf...@cdw.comwrote:
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

May 3 '07 #9

"Fred" <it****@cdw.coma écrit dans le message de news:
rv******************************@comcast.com...
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
May 3 '07 #10
On 5ÔÂ3ÈÕ, ÉÏÎç11ʱ04·Ö, Fred <itf...@cdw.comwrote:
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.

May 3 '07 #11
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
>
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.
May 4 '07 #12
On Thu, 03 May 2007 19:39:51 -0500, Joe Estock
<je*****@NOSPAMnutextonline.comwrote in comp.lang.c:
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
};
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. */
}
};

... 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
May 4 '07 #13

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

Similar topics

9
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I...
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
6
by: yezi | last post by:
Hi: How to code for operation between 2 structures? The structure's defination is : struct sockaddr_in my_addr; I need call a subfunction and pass the value in my_addr to the subfuncition...
0
by: Pravin | last post by:
I have following architecture. c# code --> managed c++ code (wrapper) -> c wrapper code to third party tool --> third party tool written in c. c# code sends some data and a delegate to c++...
1
by: Kelvin | last post by:
to All, I'm a newbie in VC++, can anyone tell me about the performance in various Data Structures (eg. Enum, map, list, vector, hash, arrayList)? I would like to know performance on CPU and...
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
2
by: guy.gorodish | last post by:
hi, i have couple of qeustions: 1. I have built a interop of c dll. In this dll i have a function that expect a pointer to a struct. I want to pass a struct from my c# interop into this c...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Have a complex process where I need to Import a large amount of data then run some transformations on this data then import into DataBase. The transformation involves multiple fields and multiple...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.