0

Possible Duplicate:
How do function pointers in C work?

Surfing on stackoverflow I found this example:

/* Validation functions start */
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue();
}

int getNextRandomValue(void)
{
    return rand();
}

int main(void)
{
    int myarray[10];
    populate_array(myarray, 10, getNextRandomValue);
    ...
}

I was wondering, imagine getNextRandomValue had a parameter getNextRandomValue(int i), how would I include this and making the function accepting inputs?

Many thanks

0

3 Answers 3

4

Common practice is to pass a pointer to "data" together with the function. When function gets called, pass that "data" pointer into function and assume that the function itself knows what to do with that data. In fact the data is usually a pointer to a structure. So the code looks like this:

struct func1_data {
    int a;
    int b;
};

struct func2_data {
    char x[10];
};

int function1(void *data) {
    struct func1_data *my_data = (typeof(my_data)) data;
    /* do something with my_data->a and my_data->b */
    return result;
}

int function2(void *data) {
    struct func2_data *my_data = (typeof(my_data)) data;
    /* do something with my_data->x */
    return result;
}

and assume we have

int caller(int (*callback), void *data) {
    return callback(data);
}

Then you call all this like this:

struct func1_data data1 = { 5, 7 };
struct func2_data data2 = { "hello!" };
caller(function1, (void *) &data1);
caller(function2, (void *) &data2);
Sign up to request clarification or add additional context in comments.

Comments

2

It's probably a good idea to get familiar with function-pointer syntax. You need to change the argument to int (*getNextValue)(int).

7 Comments

What if I don't know how many parameters need? What happens if sometimes I want to pass callbacks that have one values and others that have 3 ?
@haskellguy: You must know. Otherwise how would you call it?
consider I have a callback doThisOne(int) and doThisTwo(int, int)
@haskellguy: I understand what you're describing, but in practice it doesn't make sense. How could you call a function if you didn't know how many arguments it took?
Variadic function pointer for @haskellguy at lemoda.net/c/function-pointer-ellipsis/index.html, although it may be somewhat esoteric.
|
2

Then your code should be like this...

void populate_array(int *array, size_t arraySize, int (*getNextValue)(unsigned int))
{
    unsigned int seedvalue = 100;

    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue(seedvalue);
}

int getNextRandomValue(unsigned int seed)
{
    srand(seed);
    return rand();
}

int main(void)
{
    int myarray[10];
    populate_array(myarray, 10, getNextRandomValue);
    ...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.