Passing a function with a variable parameter as a void pointer and calling it

I have a function that I want to pass through various functions

int func1(char *ptr) { printf("%s",ptr); return 0; } 

and other functions in which I want to call func1

 int func2(void *i) { //call i //here i point to function func1 //and do something with return value of i } 

So, how do I call it main ()?

 int main() { void *d; //SOMETHING wrong in next four line d=&func1("abcd"); func2(d); d=&func1("xyz"); func2(d); return 0; } 
+4
source share
5 answers

You can simply create a function ( func2 ) that takes a pointer to the function you want to call, and another parameter that takes a pointer to your string. Then in func2 call the str argument passed in the function pointer.

 #include <stdio.h> void func1(const char *str) { puts(str); } void func2(void (*fp)(const char *), const char *str) { fp(str); } int main(void) { const char *str = "Hello world."; func2(func1, str); return 0; } 
+3
source

First of all, you should use function pointers to complete the desired task. Using the void pointer is a method in which you should know what exactly you will use for dereferencing. Since you are passing the address of the function, you must always refer it to a function pointer. So avoid all this and declare a func pointer anyway. Use void * only if you know you can handle all the features. eg. memcpy uses void * this is acceptable as you just want a bunch of data at the sum location to be copied to the destination.

 #include <stdio.h> void func1(char * str) { printf("%s",str); } void func2(void * g) { void (*p) (char *); //function pointer p = (void (*)(char *))g;//casting void * to pi.e func pointer reqiuired eitherways. p("Func pointer caller"); } void main() { void * d = func1; // name of the function is itselt its address //printf("func1 = %d\nd = %d",func1,d); func2(d); } 

all this can be avoided by a simple function pointer.

+2
source

First of all: a function pointer refers only to a function. Parameters enter the game when a function is called, either directly or through a function pointer.

So, if you want to achieve what you mean, it is binding (set) of parameters to a specific varaible function, you need to use a wrapper function:

 int func1(char * pc) { int i; /* assigne somehting to "i" and use "pc" */ return i; } int func1_abcd(void) { return func1("abcd") } int func1_xyz(void) { return func1("xyz") } typedef int (*pfunc1_wrapper_t)(void); int func2(pfunc1_wrapper_t pfunc1_wrapper) { return pfunc1_wrapper(); } int main(int argc, char ** argv) { pfunc1_wrapper_t pfunc1_wrapper = NULL; int i = 0; pfunc1_wrapper = func1_abcd; i = func2(pfunc1_wrapper); pfunc1_wrapper = func1_xyz; i = func2(pfunc1_wrapper); ... return 0; } 
+2
source

Refer to the function pointers and read the Wiki function pointers and you will learn how to fix your code.

+1
source

Jatin has already posted a generic link to function pointers that explain function pointers very well.

How do you, seam, want to pass not a classic function pointer, but a function pointer With its argument as one “function pointer” to another function, I don’t think it will work.

But why pass a function with its argument as one parameter? This would mean that the function you pass with the parameters will return only one result, which is already known outside of func2 (since func2 will call func1 with this already outside of a certain set of parameters). So, why not just pass the result of the func1 call to func2?

0
source

All Articles