Yes, this is my "plan B" solution :-)
Isn't there any possibility to perform some kind of direct cast ?
Anyway,
thanks for your suggestion....
Main Topics
Browse All TopicsI have lot of fundamental mathematical/financial functions in my project. What I need is to receive name of a certain function and "convert" it to appropriate function pointer.
Example:
I have functions called f01, f02, etc...
I receive function's name in a string (e.g. char* func = "f08")
I would like to directly convert the name to a pointer to function.
Is that possible?
The goal is to avoid something like:
if (!strcmp(line_Func, "f01"))
(arrGide + i)->eleFunc = &f01;
else if (!strcmp(line_Func, "f02"))
(arrGide + i)->eleFunc = &f02;
etc.....
Thanks a lot for your help...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> Isn't there any possibility to perform some kind of direct cast ?
There's no relation between the function name and the function pointer in the binary, unless you explicitly add that relation (by using a map for example).
If, however, you know the strings at compile time, you can just avoid having to look it up. But I assume you'll only know the strings at run time ?
Zoppo,
my primary goal is performance. Therefore I need as optimum solution as possible...
Yes, if there's no possibility to cast function's name to a pointer directly, I'll have to use mapping.
Infinity08,
thanks for your code. Just want to ask - direct casting to a pointer is impossible in C/C++, right?
>> direct casting to a pointer is impossible in C/C++, right?
What do you mean ? Cast a string to a pointer ? A char* is already a pointer, but it won't be the function address - it will be the address of the first character in the string.
>> my primary goal is performance. Therefore I need as optimum solution as possible...
You might want to consider not using strings as function identifiers, but maybe integer values ... Or is that an externally imposed limitation ?
>>>> my primary goal is performance.
The time for parsing an algorithm and check it's correctness normally is a multiple of that one of the function calls will need. That includes the lookup in a map as well.
There is an issue when using function pointers for different logical tasks: these functions should (must) have all the same prototype, i. e. the same return type and arguments.
>> >> What's the difference in using integers as input values?
>>
>> A string comparison is more costly than an integer comparison ;) So, you can save a few cycles there ...
Plus you can of course use the integer directly as an index in an array of function pointers, which avoids having to search for the correct value.
>>>> use the integer directly as an index in an array of function pointers
enum { F_UNK, F01, F02, F03, F04, F05, F06, F07, F08, F_MAX };
typedef double (*MyFunc)(double);
const MyFunc allFuncs[F_MAX] = { NULL, &f01, &f02, &f03, ..., &f08 };
...
fn = atoi(&line_Func[1]);
if (fn > 0 && fn < F_MAX)
(arrGide + i)->eleFunc = allFuncs[fn];
Business Accounts
Answer for Membership
by: Infinity08Posted on 2008-03-18 at 03:59:54ID: 21149680
>> What I need is to receive name of a certain function and "convert" it to appropriate function pointer.
The easiest way is to store a mapping of function name to function pointer ...