函数指针类型

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int (*calculate_sum_fun)(int a, int b);

typedef struct handle_t {
    calculate_sum_fun  calculate_sum;
} handle_t;


int add(int a, int b)
{
    return (a + b);
}

int main(void)
{
    struct handle_t *handle = NULL;
    int result = -1;
    
    handle = (struct handle_t*)malloc(sizeof(struct handle_t));
    if(handle == NULL){
        printf("memory alloc failed!!!\n");
        return -1;
    }
    
    memset(handle, 0, sizeof(struct handle_t));
    handle->calculate_sum = add;
    
    if(handle->calculate_sum != NULL){
        result = handle->calculate_sum(100, 20);
        printf("result is %d\n", result);
    }
        
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiezhi123456/article/details/81563037
今日推荐