Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction pointers, design
    primarykey
    data
    text
    <p>I am in a problem with a design question in C.</p> <p>Let's say that I have a pretty large amount of functions, with different argument count.</p> <p>POQ:</p> <pre><code>int print_one(int x) { printf("one: %d\n", x); return 1; } int print_three(int x, int y, int z) { printf("three: %d-%d-%d\n", x, y, z); return 3; } </code></pre> <p>Now, I want to connect some properties to these functions in a structure, so that I can manipulate them without knowing the exact function, including their parameter count (I might even call the structure interface)</p> <p>I tryd it like this, (&amp; I think is pretty wrong):</p> <pre><code>typedef int (*pfunc)(int c, ...); typedef struct _stroffunc { pfunc myfunction; int flags; int some_thing_count; int arguments[10]; int argumentcount; } stroffunc; int main() { stroffunc firststruct; firststruct.pfunc = (pfunc) print_two; firststruct.something_count = 101; arguments[0] = 102; argumentcount = 1; flag &amp;= SOME_SEXY_FLAG; // now I can call it, in a pretty ugly way ... however I want (with patially random results ofc) firststruct.pfunc(firststruct.arguments[0]); firststruct.pfunc(firststruct.arguments[0], 124, 11); firststruct.pfunc(1, firststruct.arguments[0], 124, 1, 1); } </code></pre> <p>I find this solution very ugly, &amp; I think (hope) that there is a better solution for calling &amp; and setting the function pointers.</p> <p>I'm just hoping, that I was clear enough ... NOTE: I didn't compile this code, but i compiled &amp; run a very similar one so the concepts are working. NOTE: pure C needed</p>
    singulars
    1. This table or related slice is empty.
    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