How to call appropriate functions?

Discussion in 'C Programming' started by Joona I Palaste, Oct 13, 2003.

  1. Ganesh Kundapur <> scribbled the following:
    > Hi all,
    > suppose i have 100 functions such as f1, f2,...f100 in a single
    > file including main. If i pass arguement to main such as
    > $a.out f1
    > main should call appropriate functions. I can do this using switch
    > statement, but if i have 1000 functions, then i have to write 1000 cases
    > OR i can use pointer to functions. I am passing string to main, how to
    > map this string to function efficiently.


    If they're all named f{i} where {i} is an integer value from 1 to 100,
    then you can use strtol() to find out the value of this integer, and use
    it to index an array of function pointers. I don't see any easier way.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
    - Bob Wiley
     
    Joona I Palaste, Oct 13, 2003
    #1
    1. Advertising

  2. Hi all,
    suppose i have 100 functions such as f1, f2,...f100 in a single
    file including main. If i pass arguement to main such as
    $a.out f1
    main should call appropriate functions. I can do this using switch
    statement, but if i have 1000 functions, then i have to write 1000 cases
    OR i can use pointer to functions. I am passing string to main, how to
    map this string to function efficiently.
     
    Ganesh Kundapur, Oct 13, 2003
    #2
    1. Advertising

  3. Joona I Palaste

    CBFalconer Guest

    Joona I Palaste wrote:
    > Ganesh Kundapur <> scribbled the following:
    > >
    > > suppose i have 100 functions such as f1, f2,...f100 in a single
    > > file including main. If i pass arguement to main such as
    > > $a.out f1
    > > main should call appropriate functions. I can do this using
    > > switch statement, but if i have 1000 functions, then i have to
    > > write 1000 cases OR i can use pointer to functions. I am
    > > passing string to main, how to map this string to function
    > > efficiently.

    >
    > If they're all named f{i} where {i} is an integer value from 1
    > to 100, then you can use strtol() to find out the value of this
    > integer, and use it to index an array of function pointers. I
    > don't see any other way.


    That does nicely for trying the system out, including getting the
    initialization of the function pointers and declaration of the
    function type right. Then the OP can improve on it by changing
    the array to an array of struct, where the struct holds the
    function pointer and a pointer to its name. Then a sequential
    search handles arbitrary input strings.

    If you pass argv and argc on down, the functions can continue the
    process with argv[2], after checking for existence.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 13, 2003
    #3
  4. Joona I Palaste

    Derk Gwen Guest

    Ganesh Kundapur <> wrote:
    # Hi all,
    # suppose i have 100 functions such as f1, f2,...f100 in a single
    # file including main. If i pass arguement to main such as
    # $a.out f1
    # main should call appropriate functions. I can do this using switch
    # statement, but if i have 1000 functions, then i have to write 1000 cases
    # OR i can use pointer to functions. I am passing string to main, how to
    # map this string to function efficiently.

    Use a program generator. Write a program that writes the source file.
    ...
    fputs(
    "#include <stdio.h>\n"
    "int main(int argc,char **argv) {\n"
    ...
    ,stdout);
    for (i=1,else=""; i<1000; i++,else="else ") {
    printf("%sif (strcmp(\"f%d\",argv[1])==0) f%d();",else,i,i);
    }
    ...

    If you're using make, you can include program generator
    xyz: xyz.o
    cc -o xyz xyz.o
    xyz.o: xyz.c pqr.h
    cc -c xyz.c
    xyz.c: xyz-gen
    xyz-gen >xyz.c
    xyz-gen: xyz-gen.c
    cc -o xyz-gen xyz-gen.c

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    Don't say anything. Especially you.
     
    Derk Gwen, Oct 14, 2003
    #4
  5. Joona I Palaste

    John Bode Guest

    Ganesh Kundapur <> wrote in message news:<>...
    > Hi all,
    > suppose i have 100 functions such as f1, f2,...f100 in a single
    > file including main. If i pass arguement to main such as
    > $a.out f1
    > main should call appropriate functions. I can do this using switch
    > statement, but if i have 1000 functions, then i have to write 1000 cases
    > OR i can use pointer to functions. I am passing string to main, how to
    > map this string to function efficiently.


    You're going to have to implement a lookup table of some sort. If all
    of the called functions have the same return type and number of
    parameters, you could do something like the following (probably not
    the best way of doing it, but I'm doing this off the top of my head):

    void f1 (void) {...}
    void f2 (void) {...}
    void f3 (void) {...}
    ....
    void f100 (void) {...}

    struct fassoc {
    char name[5]; /* long as longest function name + 1 */
    void (*fptr)(void); /* pointer to function */
    };

    struct fassoc fassoc_table[100] =
    { {"f1", f1}, {"f2", f2}, {"f3", f3}, ..., {"f100", f100} };

    void call_func_by_name (char *name)
    {
    int i;

    for (i = 0; i < 100; i++)
    {
    if (strcmp (name, fassoc_table.name) == 0)
    {
    fassoc_table.fptr ();
    break;
    }
    }
    }

    int main (int argc, char **argv)
    {
    if (argc == 2)
    {
    call_func_by_name (argv[1]);
    }

    return 0;
    }

    If the functions have different return types or take different numbers
    of parameters, then things get a bit trickier. You'd still need a
    lookup table, but now you'd need to accomodate several types of
    function pointers, and I'm not sure if you could initialize it like
    above (you'd probably have to call a function at program startup to
    set up the lookup table).
     
    John Bode, Oct 14, 2003
    #5
    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. =?Utf-8?B?VGltOjouLg==?=

    Simple Question on appropriate control???

    =?Utf-8?B?VGltOjouLg==?=, Jul 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    296
    Teemu Keiski
    Jul 15, 2004
  2. Nelson

    Which event is appropriate....

    Nelson, Nov 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    452
    Nelson
    Nov 8, 2004
  3. =?Utf-8?B?a2Fub25lcw==?=

    This header must be modified with the appropriate property

    =?Utf-8?B?a2Fub25lcw==?=, Dec 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    705
    =?Utf-8?B?a2Fub25lcw==?=
    Dec 3, 2004
  4. Xiangliang Meng
    Replies:
    1
    Views:
    1,631
    Victor Bazarov
    Jun 21, 2004
  5. beginner
    Replies:
    7
    Views:
    559
    Alex Martelli
    Aug 19, 2007
Loading...

Share This Page