How to pass various structures to function?

Discussion in 'C Programming' started by Fred, May 3, 2007.

  1. Fred

    Fred Guest

    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
     
    Fred, May 3, 2007
    #1
    1. Advertising

  2. In article <>,
    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?


    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
    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
     
    Dave Vandervies, May 3, 2007
    #2
    1. Advertising

  3. 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.
     
    Martin Ambuhl, May 3, 2007
    #3
  4. Fred

    Fred Guest

    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.
     
    Fred, May 3, 2007
    #4
  5. On May 2, 10:04 pm, 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.
    >
    > -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.
     
    Chris Johnson, May 3, 2007
    #5
  6. Fred

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, May 3, 2007
    #6
  7. 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.
     
    Richard Heathfield, May 3, 2007
    #7
  8. Fred

    Flash Gordon Guest

    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
     
    Flash Gordon, May 3, 2007
    #8
  9. On 3 May, 04:31, 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".

    >
    > > 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
     
    Nick Keighley, May 3, 2007
    #9
  10. "Fred" <> a écrit dans le message de news:
    ...
    > 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
     
    Xavier Serrand, May 3, 2007
    #10
  11. Fred

    kingfox Guest

    On 5ÔÂ3ÈÕ, ÉÏÎç11ʱ04·Ö, 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:
    >

    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.
     
    kingfox, May 3, 2007
    #11
  12. Fred

    Joe Estock Guest

    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.
    >
     
    Joe Estock, May 4, 2007
    #12
  13. Fred

    Jack Klein Guest

    On Thu, 03 May 2007 19:39:51 -0500, Joe Estock
    <> wrote 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
     
    Jack Klein, May 4, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,813
    Eric Sosman
    Jun 11, 2004
  2. Felix Kater
    Replies:
    7
    Views:
    265
    Albert van der Horst
    Dec 11, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    740
    Emmanuel Delahaye
    Sep 24, 2005
  4. StephQ
    Replies:
    5
    Views:
    307
    Michael DOUBEZ
    Feb 28, 2007
  5. q-rious
    Replies:
    2
    Views:
    855
    Manuel Collado
    Mar 13, 2011
Loading...

Share This Page