1

Let's assume, I have a function with a prototype like (in function.h)

void function(int argument, bool flag);

This function has two variant behaviours, dependening on flag.

Early on, in a module (module.h, module.c) I called that function:

#include "function.h"

function(argument1, true);
function(argument2, false);

Let's further assume, I want to pass this function into a module, inverting that dependencies:

void (* funcPtr)(int argument, bool flag);

void setFuncPtr(void (* func)(int argument, bool flag))
{
  funcPtr = func;
}

Now I can have in function.c:

#include "module.h"

setFuncPtr(function);

And in module.h:

funcPtr(argument1, true);
funcPtr(argument2, false);

Is there a way to have two pointers, each pointing to the function, but with different, hardcodes values for flag? Like:

void (* funcPtrTrue)(int argument, bool flag);
void (* funcPtrFals)(int argument, bool flag);
/* Or, even better
void (* funcPtrTrue)(int argument);
void (* funcPtrFals)(int argument);
*/

void setFuncPtr(void (* func)(int argument, bool flag))
{
  funcPtrTrue  = func(argument, true);
  funcPtrFalse = func(argument, false);

}

(I now, this is no correct code, but should illustrated the desired functionality)

After Olis proposal, let's complicate things. bool is a typedef in function.h:

typedef enum {
  FALSE,
  TRUE
} bool;

Now I have to include function.h again to module.c in order to make it work again.

#include "function.h"

void functionTrue(int argument)
{
  fktPtr(argument, TRUE);
}
1
  • Nope, not possible. A function pointer is an address. It doesn't hold any more information in it, as argument checking is compile-time, not run-time. Use wrappers, like Oli suggested. Commented Mar 25, 2013 at 12:40

2 Answers 2

4

C doesn't have default arguments.

I assume there's some good reason you want two separate function pointers, rather than just directly calling funcPtr with the required flag parameter. If so, why not just have two wrapper functions inside your module?

void (* funcPtr)(int argument, bool flag);

void setFuncPtr(void (* func)(int argument, bool flag)) {
  funcPtr = func;
}

void funcTrue(int argument)  { funcPtr(argument, true); }
void funcFalse(int argument) { funcPtr(argument, false); }
Sign up to request clarification or add additional context in comments.

7 Comments

Okay, that helps alot. Just one more point. Suppose, flag is not a bool but a typedef enum. Now I need to include that typedef somehow, but with the function-pointer setter I wanted to invert exact that dependency..
@Oliver: So do you want your module to have a public API function along the lines of setFuncPtr(funcPtr, argToUse)?
I'm not sure I understand what you mean. I want to hide enumeration, that controls the function. function.h should be hidden and the API of module.h should be usable without function.h. I want to be able to replace function.h without changing the interface of module.h.
@Oliver: What I mean is, how do you intend to inject the value of flag?
Ah, I see. As for the problem, where the question rised: Not at all. I only need one behavior of the function, therefore only one value for flag. This is why I didn't want to expose the whole interface with the enumeration to the "outside".
|
1

you can use structures

struct funcstruct {
    void (*func)(int arg, bool flag);
    bool default_flag;
} funcstruct;

struct funcstruct funcPtrTrue, funcPtrFalse;

void setFuncPtr(void (* func)(int argument, bool flag))
{
  funcPtrTrue.func = func; funcPtrTrue.default_flag = true;
  funcPtrFals.func = func; funcPtrFals.default_flag = false;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.