Structures and Variable Functions

Discussion in 'C Programming' started by kelvSYC, Sep 14, 2003.

  1. kelvSYC

    kelvSYC Guest

    Can variable functions be implemented in C, and if so, how?

    Also, can you have a function or a function prototype as a member in a
    structure?

    --
    I am only a mirage.
    --
    comp.lang.c.moderated - moderation address:
    kelvSYC, Sep 14, 2003
    #1
    1. Advertising

  2. kelvSYC

    Ben Pfaff Guest

    kelvSYC <> writes:

    > Can variable functions be implemented in C, and if so, how?


    Please define "variable function".

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    No, but you may have a pointer to a function.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Sep 14, 2003
    #2
    1. Advertising

  3. kelvSYC wrote:

    > Can variable functions be implemented in C, and if so, how?


    I checked the index in Maclennan, and the index in the Dragon book, for
    "variable function". No dice. What do you mean by it?

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    You can have a function pointer as a structure member.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    --
    comp.lang.c.moderated - moderation address:
    Richard Heathfield, Sep 14, 2003
    #3
  4. kelvSYC

    Derk Gwen Guest

    kelvSYC <> wrote:
    # Can variable functions be implemented in C, and if so, how?

    Yes.

    # Also, can you have a function or a function prototype as a member in a
    # structure?

    Yes.

    (cd /tmp
    cat <<':eof' >t.c
    #include <stdio.h>
    #include <stdlib.h>
    typedef int (*func)(char *s);
    typedef struct {func f; char *s;} object;

    int X(char *s) {printf("X-%s-X\n",s); return 1;}
    int Y(char *s) {printf("Y-%s-Y\n",s); return 2;}

    object choose(char *s) {
    object o; o.s = s;
    switch (*s) {
    case 'x': o.f = X; break;
    case 'y': o.f = Y; break;
    }
    return o;
    }
    int apply(object o) {return o.f(o.s);}

    int main(int N,char **P) {
    int r = apply(choose(P[1]));
    printf("return %d\n");
    return 0;
    }
    :eof
    cc t.c
    a.out xyzzy)

    X-xyzzy-X
    return 0

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    But I do believe in this.
    --
    comp.lang.c.moderated - moderation address:
    Derk Gwen, Sep 14, 2003
    #4
  5. kelvSYC

    Jack Klein Guest

    On 14 Sep 2003 01:28:31 GMT, kelvSYC <> wrote
    in comp.lang.c:

    > Can variable functions be implemented in C, and if so, how?


    What do you mean by a variable function? If you mean a function like
    printf(), that accepts a variable number and type of arguments, the
    standard header <stdarg.h> provides macros for writing such functions.

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    You can have a pointer to a function as a member of a structure.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    --
    comp.lang.c.moderated - moderation address:
    Jack Klein, Sep 14, 2003
    #5
  6. kelvSYC

    pete Guest

    kelvSYC wrote:
    >
    > Can variable functions be implemented in C, and if so, how?


    I don't know what a variable function is.

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    You can have a pointer to a function, as a member in a structure.

    --
    pete
    --
    comp.lang.c.moderated - moderation address:
    pete, Sep 14, 2003
    #6
  7. kelvSYC wrote:
    > Can variable functions be implemented in C, and if so, how?


    What specifically do you mean by "variable function"?

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    Functions live in a different address space than data, so no.
    Quite probably you should consider using a *pointer* to a
    function; the pointer lives in data space.
    --
    comp.lang.c.moderated - moderation address:
    Douglas A. Gwyn, Sep 14, 2003
    #7
  8. kelvSYC wrote:
    > Can variable functions be implemented in C, and if so, how?


    Which is exactly what ? Not being sure what you mean, I suggest taking a
    look at the way varargs work (e.g. in printf()). Another thing to look at
    are function-pointer.

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    No. What for, anyways ? If you want a _member_function, use C++.

    Uli

    --
    Questions ?
    see C++-FAQ Lite: http://parashift.com/c -faq-lite/ first !
    --
    comp.lang.c.moderated - moderation address:
    Ulrich Eckhardt, Sep 14, 2003
    #8
  9. In article <>, kelvSYC
    <> writes
    >Can variable functions be implemented in C, and if so, how?
    >
    >Also, can you have a function or a function prototype as a member in a
    >structure?


    Not in C, though you could have a function pointer if that is any help
    to you.


    --
    Francis Glassborow ACCU
    64 Southfield Rd
    Oxford OX4 1PA +44(0)1865 246490
    All opinions are mine and do not represent those of any organisation
    --
    comp.lang.c.moderated - moderation address:
    Francis Glassborow, Sep 14, 2003
    #9
  10. In 'comp.lang.c', kelvSYC <> wrote:

    > Can variable functions be implemented in C, and if so, how?


    What the hell is a 'variable function'? Maybe you want a pointer to a
    function? Or are you talking about class function member? In this case, you
    want C++ instead of C.

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    No. All you can have is a pointer to a function. Once correctly initialized,
    it is callable.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    --
    comp.lang.c.moderated - moderation address:
    Emmanuel Delahaye, Sep 14, 2003
    #10
  11. In comp.lang.c.moderated kelvSYC <> wrote:
    > Can variable functions be implemented in C, and if so, how?


    Close to impossible to tell until you specify what exactly you mean by
    a "variable function".

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    No. But function pointers can.
    --
    Hans-Bernhard Broeker (-aachen.de)
    Even if all the snow were burnt, ashes would remain.
    --
    comp.lang.c.moderated - moderation address:
    Hans-Bernhard Broeker, Sep 14, 2003
    #11
  12. kelvSYC

    kelvSYC Guest

    > > Can variable functions be implemented in C, and if so, how?
    > Please define "variable function".


    Using the value of a string to call a function with the same name. For
    example, a way of calling a function named foo() using a string named
    "foo". Then, if you change "foo" to "bar", the same code would call
    bar().

    --
    I am only a mirage.
    kelvSYC, Sep 15, 2003
    #12
  13. kelvSYC wrote:

    >> > Can variable functions be implemented in C, and if so, how?

    >> Please define "variable function".

    >
    > Using the value of a string to call a function with the same name. For
    > example, a way of calling a function named foo() using a string named
    > "foo". Then, if you change "foo" to "bar", the same code would call
    > bar().


    FAQ 20.6 - see my sig block for FAQ URL.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Sep 15, 2003
    #13
  14. kelvSYC

    John Bode Guest

    kelvSYC <> wrote in message news:<140920031912546655%>...
    > > > Can variable functions be implemented in C, and if so, how?

    > > Please define "variable function".

    >
    > Using the value of a string to call a function with the same name. For
    > example, a way of calling a function named foo() using a string named
    > "foo". Then, if you change "foo" to "bar", the same code would call
    > bar().


    Short answer: no.

    Long answer: not directly. You'd have to implement a lookup table
    that contains both the string and the function pointer. You'd search
    the table for the string, then call the function using the associated
    pointer.

    Example:

    /*
    ** Assuming all the functions you call return void, and
    ** take no arguments; simplest case
    */
    typedef void (*fptr)(void);

    /*
    ** Lookup table associating function name string
    ** with function pointer:
    */
    struct flookup {
    char fname[FNAMELEN];
    fptr func;
    };

    void foo (void) {...}
    void bar (void) {...}

    struct flookup lkuptable[NFUNCS] = {"foo", foo, "bar", bar, ...};

    void CallByName (char *fname)
    {
    int i = 0;

    for (i = 0; i < NFUNCS; i++)
    {
    if (!strcmp (fname, lkuptable.fname))
    {
    lkuptable.func ();
    break;
    }
    }
    }

    Things get a bit trickier if you have to support functions with
    different prototypes. You'd have to use a union or something to
    handle the different function types, and you'd have to find a way to
    pass the necessary arguments to the CallByName function. In all, the
    bookkeeping gets to be more trouble than it's worth.
    John Bode, Sep 15, 2003
    #14
  15. kelvSYC wrote:

    >>>Can variable functions be implemented in C, and if so, how?

    >>
    >>Please define "variable function".

    >
    >
    > Using the value of a string to call a function with the same name. For
    > example, a way of calling a function named foo() using a string named
    > "foo". Then, if you change "foo" to "bar", the same code would call
    > bar().
    >


    You have two choices: cascading "if-else" ladder and a table.
    You have to associate a function pointer with a string.

    void Execute_Function(const char * const name)
    {
    if (strcmp(name, "foo") == 0)
    Foo();
    else if (strcmp(name, "bar") == 0)
    Bar();
    // Und so weite.
    }

    Or:
    typedef void (*FUNC_PTR)(void);

    struct Func_Record
    {
    const char * name;
    FUNC_PTR function;
    };

    struct Func_Record func_table[] =
    {
    {"foo", Foo}, {"bar", Bar}
    };
    const unsigned int NUM_FUNCS = sizeof(func_table)
    / sizeof(func_table[0]);

    void Execute_Function(const char * const name)
    {
    unsigned int i;
    for (i = 0; i < NUM_FUNCS; ++i)
    {
    if (strcmp(func_table.name, name) == 0)
    {
    func_table.function();
    break;
    }
    }
    return;
    }


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Sep 15, 2003
    #15
  16. kelvSYC

    John Potter Guest

    On 14 Sep 2003 01:28:31 GMT, kelvSYC <> wrote:

    > Also, can you have a function or a function prototype as a member in a
    > structure?


    Just adding some amusement to your question.

    typedef int Func (void); /* What is this? */
    int g (Func func) { /* It is a pointer to a function */
    return func();
    }
    int f () {
    return 42;
    }
    struct S {
    Func f1; /* It is a function. Ok C++ but not C */
    Func* f2; /* This is a pointer to a function */
    };
    int main () {
    g(f); /* valid */
    g(&f); /* valid */
    }

    Did this have anything to do with your question?

    John
    --
    comp.lang.c.moderated - moderation address:
    John Potter, Sep 16, 2003
    #16
  17. kelvSYC

    kelvSYC Guest

    > > Also, can you have a function or a function prototype as a member in a
    > > structure?

    > You can have a pointer to a function as a member of a structure.


    Can you please explain how that would work (as none of my references
    mention this sort of thing)?

    --
    I am only a mirage.
    --
    comp.lang.c.moderated - moderation address:
    kelvSYC, Sep 16, 2003
    #17
  18. kelvSYC

    LibraryUser Guest

    "Douglas A. Gwyn" wrote:
    > kelvSYC wrote:
    >
    > > Can variable functions be implemented in C, and if so, how?

    >
    > What specifically do you mean by "variable function"?
    >
    > > Also, can you have a function or a function prototype as a
    > > member in a structure?

    >
    > Functions live in a different address space than data, so no.
    > Quite probably you should consider using a *pointer* to a
    > function; the pointer lives in data space.


    I think you may want to reconsider that statement. On some
    machines it may be so, but it is not part of the standard, and
    the namespace is the same. However the only actions that may be
    performed on a function is to call it or to form a pointer to it,
    which can in turn be used to call it. Which is equally unclear.

    --
    Replies should be to the newsgroup
    Chuck Falconer, on vacation.
    --
    comp.lang.c.moderated - moderation address:
    LibraryUser, Sep 16, 2003
    #18
  19. On 16 Sep 2003 07:14:56 GMT, kelvSYC <> wrote:

    >> > Also, can you have a function or a function prototype as a member in a
    >> > structure?

    >> You can have a pointer to a function as a member of a structure.

    >
    >Can you please explain how that would work (as none of my references
    >mention this sort of thing)?


    Having a structure with a pointer to a function as a member is
    conceptually identical to having a structure with a pointer to object
    as a member.

    struct x {
    int i;
    double d;
    int *pi;
    struct x *px;
    double (*pf)(double, double);
    } my_x;

    my_x.pf = pow;


    <<Remove the del for email>>
    --
    comp.lang.c.moderated - moderation address:
    Barry Schwarz, Sep 19, 2003
    #19
  20. LibraryUser wrote:
    > I think you may want to reconsider that statement.


    No. The model I presented is the one that makes it
    easiest to understand how to progam such things in a
    portable manner. While separate I/D space is not
    required, it is possible, so that more restrictive
    possibility is the one to use as a guideline.
    --
    comp.lang.c.moderated - moderation address:
    Douglas A. Gwyn, Sep 19, 2003
    #20
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,580
    Victor Bazarov
    Jun 21, 2004
  2. N Yiannakoulias

    structures and functions

    N Yiannakoulias, Sep 19, 2003, in forum: C Programming
    Replies:
    5
    Views:
    333
    Eric Amick
    Sep 19, 2003
  3. tweak
    Replies:
    14
    Views:
    2,769
    Eric Sosman
    Jun 11, 2004
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    1,872
    Eric Sosman
    Jan 7, 2005
  5. Alfonso Morra
    Replies:
    11
    Views:
    703
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page