function name as string

C

CptDondo

I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

--Yan
 
K

Karl Malbrain

I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

--Yan

Sure, just make a table that maps the string name to the function
pointer, look up the string name, and call the corresponding
function.

karl m
 
G

Guest

CptDondo said:
I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

When you run your program, you cannot rely on the names of functions
even being available at all, let alone having easy access to them,
sorry. You will need to save the names of all functions you wish to
call by name, and their addresses, manually.

struct function {
char *name;
void (*func)(void);
} functions[] = {
...
{ "xyz", &xyz },
...
};

You can then search in this list to see if the name exists, and if so,
call it.

name = "xyz";
for (i = 0; i != sizeof functions / sizeof *functions; i++) {
if (strcmp(functions, name) == 0) {
functions.func();
break;
}
}

(There are almost certainly better ways to search through the list,
and perhaps better ways to store the list, but the best way depends on
your needs.)
 
C

CptDondo

Harald said:
CptDondo said:
I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

When you run your program, you cannot rely on the names of functions
even being available at all, let alone having easy access to them,
sorry. You will need to save the names of all functions you wish to
call by name, and their addresses, manually.

struct function {
char *name;
void (*func)(void);
} functions[] = {
...
{ "xyz", &xyz },
...
};

You can then search in this list to see if the name exists, and if so,
call it.

name = "xyz";
for (i = 0; i != sizeof functions / sizeof *functions; i++) {
if (strcmp(functions, name) == 0) {
functions.func();
break;
}
}

(There are almost certainly better ways to search through the list,
and perhaps better ways to store the list, but the best way depends on
your needs.)


That's kind of what I figured.... I was trying to avoid the mother of
all switch statements... Or lookups, or whatever. Just trying to make
the next guy's job a bit easier.... :)

Thanks,

--Yan
 
J

Jack Klein

Harald said:
CptDondo said:
I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

When you run your program, you cannot rely on the names of functions
even being available at all, let alone having easy access to them,
sorry. You will need to save the names of all functions you wish to
call by name, and their addresses, manually.

struct function {
char *name;
void (*func)(void);
} functions[] = {
...
{ "xyz", &xyz },
...
};

You can then search in this list to see if the name exists, and if so,
call it.

name = "xyz";
for (i = 0; i != sizeof functions / sizeof *functions; i++) {
if (strcmp(functions, name) == 0) {
functions.func();
break;
}
}

(There are almost certainly better ways to search through the list,
and perhaps better ways to store the list, but the best way depends on
your needs.)


That's kind of what I figured.... I was trying to avoid the mother of
all switch statements... Or lookups, or whatever. Just trying to make
the next guy's job a bit easier.... :)

Thanks,


DO NOT use a switch statement for this. Put the entries in the array
in sorted order and use bsearch().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
W

websnarf

I think the answer to this is "no" but I figured I'd try....

I have a function name as a string:

funcName = "xyz";

and I have a function:

void xyz(blah) {}

Is there some way to call function xyz using the string contained in
funcName?

Something like (I know this is wrong)

(void)(*funcName)(blah)???

You have to have a mapping from "xyz" to void (*xyz)(). The language
doesn't provide a natural way to do this, but there is a neat and
maintainable way of doing this. In one file, lets call it funcs.i,
write the following:

funcs.i:
----------
functionInvoke(xyz)
functionInvoke(anyotherfunctionnameyoulike)
....

And from your C sources do the following:


program.c:
---------------

#define str_aux(x) #x
#define str(x) str_aux(x)
#define functionInvoke(x) str(x), &(x)
struct functionTable {
char * name;
void (* fn) ();
} fTable[] = {
#include "funcs.i"
};
#undef functionInvoke

So then you can add as many functions as you like to your table in the
funcs.i file. You can then search for the string name in fTable
amongst the names to determine which fn to call.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top