FidoNet Echomail Archive
c_echo

<<< Previous Index Next >>>

From: Darin McBride
To: All
Date: 2004-12-20 15:43:38
Subject: Re: [C] RE: C Digest, Vol 15, Issue 2

From: Darin McBride <dmcbride{at}naboo.to.org>

On December 20, 2004 11:43 am, Robert Lewis wrote:
> I wonder if someone could show me how to fill a structure (preferably,
> second choice is an array) with pointers to functions and then the
> structure of functions, that is execute each of the functions pointed in
> the structure.
>
> I just can't figure out the pointer syntax or how to pass control to the
> same. Or is this even possible?

Hope this helps:

$ cat x.c; gcc x.c -o x; ./x
#include <stdio.h>

int some_func_returning_int() { return 3; } char*
some_func_returning_p_to_char(char* a, char const* b) {
    printf("a=[%s], b=[%s]\n", a, b);
    return a;
}

struct functions
{
    int (*a_fnc)();
    char* (*b_fnc)(char*, char const*);
};

int main()
{
    struct functions z;
    z.a_fnc = some_func_returning_int;
    z.b_fnc = some_func_returning_p_to_char;

    printf("a_fnc = %d\n", z.a_fnc());
    printf("b_fnc = %s\n", z.b_fnc("blah", "foo"));
    return 0;
}
a_fnc = 3
a=[blah], b=[foo]
b_fnc = blah

--- BBBS/LiI v4.01 Flag-5
 * Origin: Prism's_Point (1:261/38.1)
SEEN-BY: 633/267 270
@PATH: 261/38 123/500 106/2000 633/267


<<< Previous Index Next >>>