1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How could i define this?

Discussion in 'C' started by inspiration, Apr 16, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    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".
     
  3. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    This could also done.


    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.
     

Share This Page