Getting address of a function through some string

Discussion in 'C Programming' started by Prashanth Badabagni, Jan 13, 2005.

  1. #include<string.h>

    void (*foobar)();
    void main()
    {

    char *str;
    strcpy(str,"Fun1");
    strcat(str,"method1");

    foobar= ( Here i should get the address of Fun1method1 function through "str")

    (*foobar)();
    }

    void Fun1Method1()
    {
    printf("Here ... I am !!!");
    }


    Can any one please help with this ....
     
    Prashanth Badabagni, Jan 13, 2005
    #1
    1. Advertising

  2. Prashanth Badabagni

    Ravi Uday Guest

    Prashanth Badabagni wrote:
    > #include<string.h>
    >
    > void (*foobar)();
    > void main()
    > {
    >
    > char *str;
    > strcpy(str,"Fun1");
    > strcat(str,"method1");
    >
    > foobar= ( Here i should get the address of Fun1method1 function through "str")


    Its not possible that way !

    Instead, what you want is:

    foobar = Fun1Method1;

    Also, if you want to print out the address of that function you
    can use
    printf ("Address of Fun1Method1 = %p\n", (void *)Fun1Method1 );
    >
    > (*foobar)();
    > }
    >
    > void Fun1Method1()
    > {
    > printf("Here ... I am !!!");
    > }
    >
    >
    > Can any one please help with this ....
     
    Ravi Uday, Jan 13, 2005
    #2
    1. Advertising

  3. Prashanth Badabagni

    dandelion Guest

    "Prashanth Badabagni" <> wrote in message
    news:...
    > #include<string.h>
    >
    > void (*foobar)();
    > void main()
    > {
    >
    > char *str;
    > strcpy(str,"Fun1");
    > strcat(str,"method1");
    >
    > foobar= ( Here i should get the address of Fun1method1 function

    through "str")
    >
    > (*foobar)();
    > }
    >
    > void Fun1Method1()
    > {
    > printf("Here ... I am !!!");
    > }
    >
    >
    > Can any one please help with this ....


    #include <stdlib.h>
    #include <string.h>

    typedef void (*fnc_ptr_t)(void);
    typedef struct fnc_entry_t
    {
    const char* name;
    fnc_ptr_t fnc;
    }fnc_entry_t;

    fnc_entry_t fnc_table[] =
    {
    { "Foo", foo },
    { "Bar", bar },
    { "Bla", bla },
    { NULL, NULL }
    }

    fnc_ptr_t
    fnc_by_name(const char* name)
    {
    int i;
    for(i=0; NULL != fnc_table.name && 0 != strcmp(name. fnc_table.name)
    ; i++)
    {
    /* nothing */
    }

    return fnc_table.fnc;
    }
     
    dandelion, Jan 13, 2005
    #3
  4. Prashanth Badabagni

    infobahn Guest

    Prashanth Badabagni wrote:
    >
    > #include<string.h>
    >
    > void (*foobar)();
    > void main()
    > {
    >
    > char *str;
    > strcpy(str,"Fun1");
    > strcat(str,"method1");
    >
    > foobar= ( Here i should get the address of Fun1method1 function through "str")
    >
    > (*foobar)();
    > }
    >
    > void Fun1Method1()
    > {
    > printf("Here ... I am !!!");
    > }
    >
    >
    > Can any one please help with this ....


    Your code has a number of problems.

    Firstly, main returns int, not void, so change the return type, and
    add return 0; at the end of main.

    Secondly, you're missing a prototype for printf, so add <stdio.h>
    to your list of includes.

    Thirdly, you reserve no storage for the target of your strcpy and
    strcat.

    Fourthly, you'll need a prototype for Fun1Method1().

    Fifthly, I think you meant to strcat "Method1", not "method1".

    Sixthly, it's a good idea to add a \n to your printf, to make
    sure the output appears when you expect it to.

    Let's solve those problems, and answer your question at the same
    time.

    #include <stdio.h>
    #include <string.h>

    void (*foobar)();
    void Fun1Method1(void);

    int main(void)
    {
    char str[12]; /* big enough for "Fun1Method1" */

    strcpy(str, "Fun1");
    strcat(str, "Method1");

    if(strcmp(str, "Fun1Method1") == 0)
    {
    foobar = Fun1Method1;
    (*foobar)();
    }

    return 0;
    }

    void Fun1Method1(void)
    {
    printf("Here ... I am !!!\n");
    }


    If you have many such functions, you may wish to consider
    constructing a lookup table, comprised of structs containing
    a string with the function name, and a pointer to the
    appropriate function:

    struct VOID_FUNCTION_VOID_LOOKUP
    {
    char Name[32]; /* based on the maximum unique identifier length
    * guaranteed by ISO C90.
    */
    void (*Function)(void);
    };

    Construct an array of these structures, populate the array,
    and sort it by name (see qsort). You can then use bsearch
    to find the right entry in the array, and dereference the
    pointer you get (if it isn't NULL) to get a pointer to the
    appropriate struct. Then it's just a matter of accessing
    the Function member of the struct.
     
    infobahn, Jan 13, 2005
    #4
  5. Prashanth Badabagni

    infobahn Guest

    Ravi Uday wrote:
    >
    > Prashanth Badabagni wrote:
    > >
    > > foobar= ( Here i should get the address of Fun1method1 function through "str")

    >
    > Its not possible that way !


    True, and the code had other problems too.

    > Instead, what you want is:
    >
    > foobar = Fun1Method1;


    Before he can do that, he'll need a declaration of Fun1Method1.

    >
    > Also, if you want to print out the address of that function you
    > can use
    > printf ("Address of Fun1Method1 = %p\n", (void *)Fun1Method1 );


    No, you can't. Function pointers are not guaranteed to be
    convertible to void *.
     
    infobahn, Jan 13, 2005
    #5
  6. On 12 Jan 2005 23:57:54 -0800, in comp.lang.c ,
    (Prashanth Badabagni) wrote:

    (a code example but no description of what he was trying to do).

    At a rough guess, you want 20.6 in the FAQ. But next time, please post a
    clear statement of your question or problem, not just some code.



    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Jan 13, 2005
    #6
  7. Mark McIntyre <> wrote in message news:<>...
    > On 12 Jan 2005 23:57:54 -0800, in comp.lang.c ,
    > (Prashanth Badabagni) wrote:
    >
    > (a code example but no description of what he was trying to do).
    >
    > At a rough guess, you want 20.6 in the FAQ. But next time, please post a
    > clear statement of your question or problem, not just some code.


    Mark,

    What i am supposed to do was ... I want to assign the address of
    some function ( whose name is not known to me ) to a function
    pointer.I would provide some standard format for the name of the
    function.The users must define the function with the defined format
    before they use my library ( All the above code is embedded into a
    library).I found this idea very much useful with the menu
    implementation.When a user goes through a multi level menu and hits
    enter at some menu item.The corresponding function will get assigned
    at runtime to a function pointer and it will be called ultimately.For
    this to happen i have to assign the function address with out knowing
    it's name .
     
    Prashanth Badabagni, Jan 17, 2005
    #7
  8. On 12 Jan 2005 23:57:54 -0800, (Prashanth
    Badabagni) wrote:

    >#include<string.h>
    >
    >void (*foobar)();
    >void main()
    > {
    >
    > char *str;
    > strcpy(str,"Fun1");
    > strcat(str,"method1");
    >
    > foobar= ( Here i should get the address of Fun1method1 function through "str")
    >
    > (*foobar)();
    > }


    <snip>

    See the clc FAQ, question 20.6:

    20.6: If I have a char * variable pointing to the name of a function,
    how can I call that function?

    http://www.eskimo.com/~scs/C-faq/q20.6.html


    Following is an implementation of this idea:

    #include <stdio.h>
    #include <stdlib.h>

    int fcn1(int n) { return n; }
    int fcn2(int n) { return n*2; }
    int fcn3(int n) { return n*3; }

    typedef struct
    {
    int (*fcn_p)(int n); // Pointer to function "int foo(int)"
    const char *fcn_name; // Name of function as string
    }
    fpa_T;


    #define NUM_FCNS 3

    int main(void)
    {
    int i, num = 12;
    fpa_T fpa[NUM_FCNS] =
    {
    { fcn1, "fcn1" }, // Assign fcn pointers & strings here
    { fcn2, "fcn2" },
    { fcn3, "fcn3" }
    };

    for (i = 0; i < NUM_FCNS; i++)
    printf("%s(%d) = %d\n", fpa.fcn_name, num, fpa.fcn_p(num));

    return EXIT_SUCCESS;
    }


    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Jan 17, 2005
    #8
    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. avnrao
    Replies:
    1
    Views:
    7,246
    avnrao
    May 4, 2004
  2. komal
    Replies:
    6
    Views:
    1,435
    msalters
    Jan 25, 2005
  3. Timo
    Replies:
    8
    Views:
    760
    Barry Schwarz
    Dec 6, 2003
  4. ankit
    Replies:
    2
    Views:
    272
    ankit
    Jan 10, 2006
  5. moneybhai

    getting mac address through aglet

    moneybhai, Aug 16, 2009, in forum: Java
    Replies:
    13
    Views:
    820
    Arne Vajhøj
    Aug 18, 2009
Loading...

Share This Page