What's the meaning of this piece of code? void (*signal(int sig, void (*func)(int)))(int);

后端 未结 5 1833
眼角桃花
眼角桃花 2020-11-27 14:45

I came across this piece of code and completely got lost interpreting its meaning.

#include 
void (*signal(int sig, void (*func)(int)))(int);         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 15:49

    Let's take an example of how this nasty declaration could be used:

    void (*signal(int sig, void (*func)(int)))(int);
    

    Without too much verbosity, we could say that "signal" is a function with two parameters that returns a function.

    #include 
    
    // First function that could be returned by our principal function
    // Note that we can point to it using void (*f)(int)
    void car_is_red(int color)
    {
        printf("[car_is_red] Color %d (red) is my favorite color too !\n", color);
    }
    
    // Second function that could be returned by our principal function
    // Note that we can point to it using void (*f)(int)
    void car_is_gray(int color)
    {
        printf("[car_is_gray] I don't like the color %d (gray) either !\n", color);
    }
    
    // The function taken as second parameter by our principal function
    // Note that we can point to it using void (*func)(int)
    void show_car_type(int mod)
    {
        printf("[show_car_type] Our car has the type: %d\n",mod);
    }
    
    /* Our principal function. Takes two parameters, returns a function. */
    void (* show_car_attributes(int color, void (*func)(int)) )(int)
    {
        printf("[show_car_attributes] Our car has the color: %d\n",color); // Use the first parameter
    
        int mod = 11;  // Some local variable of our function show_car_attributes()
        func(mod);  // Call the function pointed by the second parameter (i.e. show_car_type() )
    
        // Depending on color value, return the pointer to one of two functions
        // Note that we do NOT use braces with function names
        if (color == 1)
            return car_is_red;
        else
            return car_is_gray;
        }
    
    
    //main() function
    int main()
    {
        int color = 2;   // Declare our color for the car
        void (*f)(int);  // Declare a pointer to a function with one parameter (int)
    
        f = show_car_attributes(color, show_car_type); // f will take the return 
               // value of our principal function. Stated without braces, the 
               // parameter  "show_car_types" is a function pointer of type 
               // void (*func)(int).
    
        f(color);  // Call function that was returned by show_car_attributes()
    
        return 0;
    }
    

    Let's see what will be output:

    If color = 1

    [show_car_attributes] Our car has the color: 1
    [show_car_type] Our car has the type: 11
    [car_is_red] Color 1 (red) is my favorite color too !
    

    If color = 2

    [show_car_attributes] Our car has the color: 2
    [show_car_type] Our car has the type: 11
    [car_is_gray] I don't like the color 2 (gray) either !
    

提交回复
热议问题