views:

354

answers:

5

Im looking to have a common parent function like so

void main (param 1, param 2)
{
    <stuff to do>
    param1();
    print("The function %s was called", param 2);
    <more stuff to do>
}

Where param 1 will be the name of the function to be called and param 2 will be some descriptive text. param 2 is easy and I have that solved, but Im unclear as to how I would call a function from the same parent function by passing in the functions name. THere are a few other things that the parent function does, but instead of having multiple parent function who only differ in the function they call or a single parent with a switch statement, id prefer if this way was possible. Any thoughts?

A: 

What language is this? if functions aren't first-class citizens in this language, you can't do it

If it's something like Java, you could get around this by passing in an object that implements an interface, and just call that function on the object.

Zack
My apologies, its C.
dangerisgo
A: 

This is not easy to do in C and compiled languages in general. On Windows I have something like this ( its been a long time ):

typedef void(* simple_void_function )()
HMODULE exe_module = GetModuleHandle( NULL ); // not sure about this
simple_void_function = GetProcAddress( exe_module, param1 );
(*simple_void_function)();

The actual function call signature can be more flexible, if you are willing to manually push args onto the stack.

Sanjaya R
I guess you are not familiar with function pointers. Check it out: http://en.wikipedia.org/wiki/Function_pointers
qrdl
Uh. That snippet includes a function pointer! Besides how exactly is he supposed to pass a function pointer to main?! He's trying to invoke a function in an executable by name. Just like run rundll.exe. Infact, if he had a DLL he could just use RUNDLL.exe
Sanjaya R
+5  A: 

What you actually want to pass for param 1 is a function pointer to the function you would like to call, as opposed to that function's name.

Have a look at this tutorial about function pointers.

tehblanx
Um. If thats really main, good luck passing a function pointer.
Sanjaya R
hah. good point!
tehblanx
A: 

I'd say that function pointers are a good thing but you may be able to leverage the pre-processor to do what you want...(learning how to use function pointers is a good thing though)...

I mean, if param2 is something that is known statically, it's pretty useless to have function pointers.. Function pointers would be better if you've dynamic values and while running your program once you want to give param2 = foo and then param2 = bar...

Do you actually need different function prototypes or could you have something like...

#ifdef BAR
 void foo() { whatever(); }
#elif defined(FOO)
 void foo() { whocares(); }
#else
 #define foo() do{} while(0)
#endif

and always have the same body but giving different -D on the command line...

LB
That would have to be recompiled every time you wanted a different argument.
Chuck
A: 

Something like this?

#include <stdio.h>
void foo(void) { printf("foo\n"); }
void bar(void) { printf("bar\n"); }
static inline void parent(void func(), const char *msg)
{
    /* do stuff */
    func();
    printf("%s called\n", msg);
    /* more stuff */
}
int main(void)
{
    parent(foo, "test1");
    parent(bar, "test2");
    return 0;
}
felipec