函数指针

#include <stdio.h>

char *test();

 

int main()

{

    char *name = test();

    

    printf("name=%s\n", name);

    

    return 0;

}

 

char *test()

{

    return "rose";

}

#include <stdio.h>

 

 

double haha(double d, char *s, int a)

{

    

}

 

/*

 掌握:

 1.看懂语法

 2.定义指向函数的指针

    double (*p)(double, char *, int);

    p = haha;

    或者

    double (*p)(double, char *, int) = haha;

 3.如何间接调用函数

 1> p(10.7, "jack", 10);

 2> (*p)(10.7, "jack", 10);

 */

 

void test()

{

    printf("调用了test函数\n");

}

 

int sum(int a, int b)

{

    return a + b;

}

 

int main()

{

    // 定义指针变量指向sum函数

    // 左边的int:指针变量p指向的函数返回int类型的数据

    // 右边的(int, int):指针变量p指向的函数有2个int类型的形参

    int (*p)(int, int);

    

    p = sum;

    

    //int c = p(10, 11);

    

    //int c = (*p)(10, 11);

    

    int c = sum(10, 9);

    

    printf("c is %d\n", c);

    

    

    return 0;

}

 

 

void test1()

{

    // (*p)是固定写法,代表指针变量p将来肯定是指向函数

    // 左边的void:指针变量p指向的函数没有返回值

    // 右边的():指针变量p指向的函数没有形参

    void (*p)();

    

    // 指针变量p指向了test函数

    p = test;

    

    p();

    //(*p)(); // 利用指针变量间接调用函数

    

    //test(); // 直接调用函数

}

posted @ 2016-03-28 09:55  lance.xiang  阅读(79)  评论(0)    收藏  举报