2

Possible Duplicates:
call a function named in a string variable in c
Dynamically invoking any function by passing function name as string

I have certain functions.. say,

double fA1B1C1( .. ) {
         ...
}

double fA2B2C3( .. ) {
         ...
}

double (*fptr)( .. );

fptr myfunc;

These functions are already defined.

So when the user inputs the string "A2B2C2", it is got into a variable and converted to "fA2B2C2"

myfunc = formatted_string; // DOES NOT WORK

But the above syntax does not work. Even typecasting it does not work.

myfunc = (fptr) formatted_string // DOES NOT WORK

But if I hard code it as

myfunc = fA2B2C2 (or) myfunc = &fA2B2C2, it works pefectly.

Where am I going wrong?

EDIT::

I tried creating a hash table and a lookup function. Like

create_hashEntry(hashtable, string, formatted_string);
// string = "A1B1C1; formatted_string = "fA1B1C1"

then the look up function

myfunc lookup_func(char *string) {
       return(get_hashEntry(hahstable, string));
}

This also failed to work.

Community
  • 1
  • 1
  • Duplicate: http://stackoverflow.com/questions/1118705/call-a-function-named-in-a-string-variable-in-c – GManNickG Aug 18 '09 at 10:18
  • 4
    You are mistaking C for a scripting language. –  Aug 18 '09 at 10:19
  • You *have* added the (name, function) pairs to the hash table, yes? (And before you run the lookup function, too?) Your code doesn't show this, only a comment. – dave4420 Aug 18 '09 at 10:46
  • If you are merely hashing a string to another string you arent going to solve any problems associated with converting a string to a memory location related to a function. Read the duplicate; pay special attention to the methods available for loading a library and using its symbol table to map a string to a function pointer. There are well developed interfaces for this on most popular platforms. – ezpz Aug 18 '09 at 10:49

4 Answers4

4

Actually, if the functions are extern, can you not do this with some super-duper evil dynamic loader trickery?

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
3

Read the duplicate, you'll find your answer.

I wanted to add, though, that this comes from a misunderstanding of how the program works.

These functions are at addresses in memory. Strings are not addresses. The names of functions, in code, are synonymous with their address. At runtime, having a string is really just having a pointer to some place in memory with characters. This has no relation to anything done at compile time, like function names.

Some languages include meta-data about the program, which means they do carry information about functions, their names, parameters, etc... at run time. C++ is not one of these languages.

This is why most answers will be a sort of simulation of this meta data: functions will be stored, and at run time you can use the string to peek into the table.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
2

You can't turn strings into function pointers in C. You have to check the contents of the string to determine which function to use.

Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
0

A string in C is a pointer to an array of chars, a function pointer is a pointer to some executable machine code. You can convert one to the other with a simple typecast, they are completly different things.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • Actually the standard does not even guarantee the ability to perform the cast, AFAIK. (i.e. the pointers can be of different sizes) – EFraim Aug 18 '09 at 11:24