Can I pass functions as parameter and the function can hold different arguments?

Think about the code where you receive the function pointer. How are you going to know how to call it?

Because different counters require different functions. Thx – user389955 Dec 2 '10 at 2:56 I don't think you understand what I asked. "How" is not answered with "because".

– Karl Knechtel Dec 2 '10 at 3:32 An answer for how to know how to call it is in my answer (add some value(s) to the structure which indicate the function type). – John Zwinck Dec 2 '10 at 4:09.

Look at va_start, va_arg, va_end. You will not be limited by two and three arguments.

Seems this applys when we have severl functions with the same name but differnt number/type of parameters. But my fucntion name is also different. So if using va_start, I need to create a function which wrap parse2parameter() and parse3parameter – user389955 Dec 4 '10 at 2:44 @user389955: it applies when you have a printf like function.It can print any number of variables you pass to it because it uses these va_arg, va_start and va_end.

The first parameter being a string constant that printf parses in order to learn about how many follow and their types, and prints them. The first two parameters in your functions appear to have same type.So if you add a string constant as a third parameter and parse it, you will be able to do all the work using simply one function. If you have reasons to not use this, showing the code will be most helpful.

– vpit3833 Dec 5 '10 at 0:03.

You can just use int(*)(), and have some other parameter(s) that describe the function signature so that you can call it. For example: typedef struct OptionValueStruct{ const char *option_name; int (*func)(); /* actually takes some arguments */ unsigned num_args; /* might need more fields to describe the args */ } OptionValueStruct_t; ... switch (value. Num_args){ case 2: ((int (*)(char*, char**))value.

Func)(opt_name, opt_val); case 3: ((int (*)(char*, char**, int))value. Func)(opt_name, opt_val, a); } Another way would be to just declare the pointer func to match parse3parameter, and then always call it as if it took three arguments, and (on some platforms), the third argument will simply be ignored. This is probably not portable though.

No. You cannot convert a function pointer to a void*, since it is not guaranteed that sizeof(function pointer) Anyway, if you want to be very precise about the whole thing, I guess you can just write the type as pointer-to-void-function and be done with it, as a pointer to one function in C is the same size as a pointer to any other function. I'll update my answer to do that.

– John Zwinck Dec 2 '10 at 4:10 ok. Thanks, I will try this way. – user389955 Dec 4 '10 at 2:41.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions