Getting address of a function through some string

  • Thread starter Prashanth Badabagni
  • Start date
P

Prashanth Badabagni

#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 ....
 
R

Ravi Uday

Prashanth said:
#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 );
 
D

dandelion

Prashanth Badabagni said:
#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;
}
 
I

infobahn

Prashanth said:
#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.
 
I

infobahn

Ravi said:
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 *.
 
M

Mark McIntyre

On 12 Jan 2005 23:57:54 -0800, in comp.lang.c , (e-mail address removed)
(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.
 
P

Prashanth Badabagni

Mark McIntyre said:
On 12 Jan 2005 23:57:54 -0800, in comp.lang.c , (e-mail address removed)
(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 .
 
R

Robert B. Clark

#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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top