<p>Let's say I'm creating a chess program. I have a function</p>

<pre><code>void foreachMove( void (*action)(chess_move*), chess_game* game); 
</code></pre>

<p>which will call the function pointer action on each valid move. This is all well and good, but what if I need to pass more parameters to the action function? For example:</p>

<pre><code>chess_move getNextMove(chess_game* game, int depth){
  //for each valid move, determine how good the move is
  foreachMove(moveHandler, game);
}

void moveHandler(chess_move* move){
  //uh oh, now I need the variables "game" and "depth" from the above function
}
</code></pre>

<p>Redefining the function pointer is not the optimal solution. The foreachMove function is versatile and many different places in the code reference it. It doesn't make sense for each one of those references to have to update their function to include parameters that they don't need.</p>

<p>How can I pass extra parameters to a function that I'm calling through a pointer?</p>

[{'answer_id': 11335, 'author': 'Antonio Haley', 'author_id': 390, 'author_profile': 'https://Stackoverflow.com/users/390', 'pm_score': 3, 'selected': False, 'text': "<p>You'd probably need to redefine the function pointer to take additional arguments.</p>\n\n<pre><code>void foreachMove( void (*action)(chess_move*, int), chess_game* game )\n</code></pre>\n"}, {'answer_id': 11363, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>Ah, if only C supported closures...</p>\n\n<p>Antonio is right; if you need to pass extra parameters, you\'ll need to redefine your function pointer to accept the additional arguments. If you don\'t know exactly what parameters you\'ll need, then you have at least three choices:</p>\n\n<ol>\n<li>Have the last argument in your prototype be a void*. This gives you flexibility of passing in anything else that you need, but it definitely isn\'t type-safe.</li>\n<li>Use variadic parameters (...). Given my lack of experience with variadic parameters in C, I\'m not sure if you can use this with a function pointer, but this gives even more flexibility than the first solution, albeit still with the lack of type safety.</li>\n<li>Upgrade to C++ and use <a href="http://en.wikipedia.org/wiki/Function_object" rel="noreferrer">function objects</a>.</li>\n</ol>\n'}, {'answer_id': 11379, 'author': 'Baltimark', 'author_id': 1179, 'author_profile': 'https://Stackoverflow.com/users/1179', 'pm_score': 2, 'selected': False, 'text': '<p>If I\'m reading this right, what I\'d suggest is to make your function take a pointer to a struct as an argument. Then, your struct can have "game" and "depth" when it needs them, and just leave them set to 0 or Null when you don\'t need them. </p>\n\n<p>What is going on in that function? Do you have a conditional that says, </p>\n\n<pre><code>if (depth &gt; -1) //some default\n  {\n  //do something\n  }\n</code></pre>\n\n<p>Does the function always REQUIRE "game" and "depth"? Then, they should always be arguments, and that can go into your prototypes. </p>\n\n<p>Are you indicating that the function only sometimes requires "game" and "depth"? Well, maybe make two functions and use each one when you need to. </p>\n\n<p>But, having a structure as the argument is probably the easiest thing.</p>\n'}, {'answer_id': 11395, 'author': 'Jesse Beder', 'author_id': 112, 'author_profile': 'https://Stackoverflow.com/users/112', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re willing to use some C++, you can use a "function object":</p>\n\n<pre><code>struct MoveHandler {\n    chess_game *game;\n    int depth;\n\n    MoveHandler(chess_game *g, int d): game(g), depth(d) {}\n\n    void operator () (chess_move*) {\n         // now you can use the game and the depth\n    }\n};\n</code></pre>\n\n<p>and turn your <code>foreachMove</code> into a template:</p>\n\n<pre><code>template &lt;typename T&gt;\nvoid foreachMove(T action, chess_game* game);\n</code></pre>\n\n<p>and you can call it like this:</p>\n\n<pre><code>chess_move getNextMove(chess_game* game, int depth){\n    //for each valid move, determine how good the move is\n    foreachMove(MoveHandler(game, depth), game);\n}\n</code></pre>\n\n<p>but it won\'t disrupt your other uses of <code>MoveHandler</code>.</p>\n'}, {'answer_id': 11442, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 1, 'selected': False, 'text': "<p>I'd suggest using an array of void*, with the last entry always void.\nsay you need 3 parameters you could do this:</p>\n\n<pre><code>void MoveHandler (void** DataArray)\n{\n    // data1 is always chess_move\n    chess_move data1 = DataArray[0]? (*(chess_move*)DataArray[0]) : NULL; \n    // data2 is always float\n    float data1 = DataArray[1]? (*(float*)DataArray[1]) : NULL; \n    // data3 is always char\n    char data1 = DataArray[2]? (*(char*)DataArray[2]) : NULL; \n    //etc\n}\n\nvoid foreachMove( void (*action)(void**), chess_game* game);\n</code></pre>\n\n<p>and then</p>\n\n<pre><code>chess_move getNextMove(chess_game* game, int depth){\n    //for each valid move, determine how good the move is\n    void* data[4];\n    data[0] = &amp;chess_move;\n    float f1;\n    char c1;\n    data[1] = &amp;f1;\n    data[2] = &amp;c1;\n    data[3] = NULL;\n    foreachMove(moveHandler, game);\n}\n</code></pre>\n\n<p>If all the parameters are the same type then you can avoid the void* array and just send a NULL-terminated array of whatever type you need.</p>\n"}, {'answer_id': 11475, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': "<p>+1 to Antonio.  You need to change your function pointer declaration to accept additional parameters.</p>\n\n<p>Also, please don't start passing around void pointers or (especially) arrays of void pointers.  That's just asking for trouble.  If you start passing void pointers, you're going to also have to pass some kind of message to indicate what the pointer type is (or types are).  This technique is <em>rarely</em> appropriate.</p>\n\n<p>If your parameters are always the same, just add them to your function pointer arguments (or possibly pack them into a struct and use that as the argument if there are a lot of parameters).  If your parameters change, then consider using multiple function pointers for the multiple call scenarios instead of passing void pointers.</p>\n"}, {'answer_id': 11661, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>If your parameters change, I would change the function pointer declaration to use the "..." technique to set up a variable number of arguments.  It could save you in readability and also having to make a change for each parameter you want to pass to the function.  It is definately a lot safer than passing void around.</p>\n\n<p><a href="http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html" rel="nofollow noreferrer">http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html</a></p>\n\n<p>Just an FYI, about the example code in the link: some places they have “n args” and others it is “n_args” with the underscore. They should all have the underscore. I thought the syntax looked a little funny until  I realized they had dropped the underscore in some places.</p>\n'}, {'answer_id': 73263, 'author': 'roo', 'author_id': 716, 'author_profile': 'https://Stackoverflow.com/users/716', 'pm_score': 0, 'selected': False, 'text': '<p>Use a typedef for the function pointer. See my <a href="https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c#9421">answer</a> for <a href="https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c">this question</a></p>\n'}, {'answer_id': 15871673, 'author': 'luser droog', 'author_id': 733077, 'author_profile': 'https://Stackoverflow.com/users/733077', 'pm_score': 0, 'selected': False, 'text': '<p>Another option would be to modify the <code>chess_move</code> structure instead of the function prototype. The structure is presumably defined in only one place already. Add the members to the structure, and fill the structure with appropriate data before any call which uses it.</p>\n'}]
['https://Stackoverflow.com/questions/11330', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']