Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to pass various structures to function?

Reply
Thread Tools

How to pass various structures to function?

 
 
Fred
Guest
Posts: n/a
 
      05-03-2007
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
 
Reply With Quote
 
 
 
 
Dave Vandervies
Guest
Posts: n/a
 
      05-03-2007
In article <(E-Mail Removed)>,
Fred <(E-Mail Removed)> 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?


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 http://www.velocityreviews.com/forums/(E-Mail Removed)
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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-03-2007
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.
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      05-03-2007
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.

 
Reply With Quote
 
Chris Johnson
Guest
Posts: n/a
 
      05-03-2007
On May 2, 10:04 pm, Fred <(E-Mail Removed)> 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.
>
> -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.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-03-2007
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
(E-Mail Removed)lid

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-03-2007
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.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-03-2007
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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-03-2007
On 3 May, 04:31, Fred <(E-Mail Removed)> 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".

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

 
Reply With Quote
 
Xavier Serrand
Guest
Posts: n/a
 
      05-03-2007

"Fred" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Questions: Merging various xml structures into one q-rious XML 2 03-13-2011 09:36 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
How do I pass structures using a C extension? timothy.williams@nvl.army.mil Python 9 03-16-2005 02:39 AM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments