C语言指针和函数

2019-06-24  本文已影响0人  一直很安静_25ae

返回指针的函数

#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(); // 直接调用函数
}

上一篇 下一篇

猜你喜欢

热点阅读