Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction pointer and default argument
    primarykey
    data
    text
    <p>Let's assume, I have a function with a prototype like (in <code>function.h</code>)</p> <pre><code>void function(int argument, bool flag); </code></pre> <p>This function has two variant behaviours, dependening on <code>flag</code>.</p> <p>Early on, in a module (<code>module.h</code>, <code>module.c</code>) I called that function:</p> <pre><code>#include "function.h" function(argument1, true); function(argument2, false); </code></pre> <p>Let's further assume, I want to pass this function into a module, inverting that dependencies:</p> <pre><code>void (* funcPtr)(int argument, bool flag); void setFuncPtr(void (* func)(int argument, bool flag)) { funcPtr = func; } </code></pre> <p>Now I can have in <code>function.c</code>:</p> <pre><code>#include "module.h" setFuncPtr(function); </code></pre> <p>And in <code>module.h</code>:</p> <pre><code>funcPtr(argument1, true); funcPtr(argument2, false); </code></pre> <p>Is there a way to have two pointers, each pointing to the function, but with different, hardcodes values for <code>flag</code>? Like:</p> <pre><code>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); } </code></pre> <p>(I now, this is no correct code, but should illustrated the desired functionality)</p> <p>After Olis proposal, let's complicate things. <code>bool</code> is a typedef in <code>function.h</code>:</p> <pre><code>typedef enum { FALSE, TRUE } bool; </code></pre> <p>Now I have to include <code>function.h</code> again to <code>module.c</code> in order to make it work again.</p> <pre><code>#include "function.h" void functionTrue(int argument) { fktPtr(argument, TRUE); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload