How do you pass a function as a parameter in C?

前端 未结 7 2241
梦谈多话
梦谈多话 2020-11-22 04:08

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C?

相关标签:
7条回答
  • 2020-11-22 04:50

    It's not really a function, but it is an localised piece of code. Of course it doesn't pass the code just the result. It won't work if passed to an event dispatcher to be run at a later time (as the result is calculated now and not when the event occurs). But it does localise your code into one place if that is all you are trying to do.

    #include <stdio.h>
    
    int IncMultInt(int a, int b)
    {
        a++;
        return a * b;
    }
    
    int main(int argc, char *argv[])
    
    {
        int a = 5;
        int b = 7;
    
        printf("%d * %d = %d\n", a, b, IncMultInt(a, b));
    
        b = 9;
    
        // Create some local code with it's own local variable
        printf("%d * %d = %d\n", a, b,  ( { int _a = a+1; _a * b; } ) );
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题