函数指针

定义数组类型:

typedef  int (arry)[10]; //或者 typedef  int arry[10]

arry myarry;    //相当于定义 int myarry[10]

myarry[0] = 1;

定义数组指针类型:

typedef int (*p_arry)[10];

int abc[12][10];
p_arry p_myarry;        //p_myarry是二维数组指针
p_myarry = abc;

c语言中的语句块:(”{}“之间的内容是语句块,在语句块的开头可以定义变量,因为c编译器要求变量的定义严格放在语句块的前端,使用语句块可以在函数中的任意地方定义变量了。实际上任然相当于在函数开头定义。)

void main()
{

    {
        int a = 3;
        printf("a=%d\n",a);
    }
    a = 1;
    b = 2;
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    {
        int b = 4;
            a = 4;
        printf("a=%d\n",a);
        printf("b=%d\n",b);
    }
    
    int b1 = 1;
    printf("b=%d\n",b);
    getchar();
}

 

定义函数类型:

typedef int (FUNC)(int);

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

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

int main()
{
    FUNC* pt = test;  //等同:FUNC* pt = &test;
    
  int result = pt(1);
  result = test(1);
  result = (*pt)(1);
void (*pf)() = &f; pf(); (*pf)(); printf("Function pointer call: %d\n", pt(3)); }
int (*fun)(int)=test;    ==>int (*fun)fun(int)=&test;

(*fun)(1);    ==>fun(1);  ==>test(1);

 

定义函数指针类型:

typedef int (*FUNC)(int);

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

int main()
{
    FUNC pt = test;
    FUNC pt_2 = &test;        //pt ==> pt_2

    int result = pt(1);
    result = (*pt_2)(1);

    printf("Function pointer call: %d\n", pt(3));
}
int (*pt)(int) = test;  ==>int (*pt)(int) =&test;

调用方式:(相同效果)

(*pt)(1) ==>pt(1) ==>test(1)

 

posted @ 2016-10-17 21:00  e-data  阅读(153)  评论(0)    收藏  举报