Hello lads,
I'd like to do the following in C:
1. The user writes a function with predefined arguments and return value in a separate file.
2. The program is compiled as my written 'main' program and the file containing the user function included.
3. When the program is invoked, the user is asked how his function is named (f.e. function1).
4. The 'main' program calls and executes the user function.
The problem is, how can I cast the string-pointer, obtained from the user to a function-pointer? (If it can be done). It is easy to define the function-pointer, because I know the form of the function, but not to assign a function to the pointer at runtime.
This code illustrates what I mean:
Main program:
char funct_name[30];
// define function pointer (argument = int, return = int)
int (*pt2Funtion)(int);
// ask the user for his function name
printf("Enter funtion name: ");
scanf("%s", funct_name);
// assign the function to the pointer
// all we know is the name of the function
HOW CAN WE DO THIS?
// invoke function through its pointer
int res = (*pt2Function)(some_int);
User file:
// function to be called
int my_funtion(int arg){
return arg*2;
}
Thanks a lot for your help.
Greetz,
Dries.