函数与指针分析实例

1.#include <stdio.h>

typedef int(FUNC)(int);

int test(int i)
{
    return i * i;
}

void f()
{
    printf("Call f()... ");
}

int main()
{
    FUNC* pt = test;
    
    void(*pf)() = &f;
    
    pf();
    (*pf)();
    
    printf("Function pointer call: %d ", pt(2));
}

2.#include <stdio.h>

typedef int(*FUNCTION)(int);

int g(int n, FUNCTION f)
{
    int i = 0;
    int ret = 0;
    
    for(i=1; i<=n; i++)
    {
        ret += i*f(i);
    }
    
    return ret;
}

int f1(int x)
{
    return x + 1;
}

int f2(int x)
{
    return 2*x - 1;
}

int f3(int x)
{
    return -x;
}

int main()
{
    printf("x * f1(x): %d ", g(3, f1));
    printf("x * f2(x): %d ", g(3, f2));
    printf("x * f3(x): %d ", g(3, f3));
}

3.#include <stdio.h>

int main()
{
    int (*p2)(int*, int (*f)(int*));
    
    int (*p3[5])(int*);
    
    int (*(*p4)[5])(int*);
    
    int (*(*p5)(int*))[5];
}

原文地址:https://www.cnblogs.com/wxb20/p/6150648.html