zoukankan      html  css  js  c++  java
  • day11基础代码——函数指针

    //

    //  main.m

    //  Demo11

    //

    //  Created by scjy on 15/10/29.

    //  Copyright © 2015年 lizhipeng. All rights reserved.

    //

    #import <Foundation/Foundation.h>

    //typedef int (*MAXV)(int ,int );//形参名可以省略

    typedef int (*MAXV)(int x,int y);//相当于把int (*)(int x,int y)定义成了MAXV

    int maxValue(int x,int y){

        return x > y ? x :y;

    }

    int sum(int x,int y) {

        

        return x + y;

    }

    int mult(int x,int y) {

        

        return x * y;

    }

    char printfhello(char str){

        return str;

    }

    void printhello0(char str){

        printf("%c ",str);

    }

    void printhello(char *str){

        printf("大家好,我叫%s ",str);

    }

    typedef int (*SUN)(char *a ,char *b);

    int sun(char *a,char *b){

        int str = strcmp(a,b);

        return str;

    }

    int getValue(int x,int y,MAXV P){

        return P(x,y);

    }

    int main(int argc, const char * argv[]) {

        @autoreleasepool {

            

            

                //*************** 第十一讲 函数指针*********************//

            

            //函数指针定义 函数是存在代码区,也是有内存地址的。

            //函数声明 函数定义 函数调用

            printf("%d ",sum(123, 456));

            printf("%p ",&sum);

            

            //函数指针定义: int(*)(int x ,int y)= null 指针定义

            //数据类型 (*指针变量名)(形式参数列表) 初值null

            

    //        char stra = 'h';

    //        char *strb =&stra;

            printf("这是一个字符:%c ",printfhello('h'));

    //定义一个可以指向上述函数的函数指针,并通过函数指针实现调用该函数。

            //函数 指针 字符串

            printhello0('h');

    //        char str0[6] = "hello";

    //        char *str =str0;

            printhello("hello");

            

    //        void (*p1)(char *str) = NULL;

    //        p1 = printhello;

    //        p1("hello");

            

            int (*p) (int x,int y) = NULL;

            p = sum;

            printf("%d ",p(7,9));

            //函数指针重指向

            p = mult;

            printf("%d ",p(5,12));

            

    //        int (*p3)(int x,int y) = NULL;

    //        p3 = maxValue;

    //        printf("%d ",p3(6,5));

            

            MAXV p3 = maxValue;

            printf("%d ",p3(60,5));

            

    //        char str4[6] = "he";

    //        char str5[6] = "llo";

    //        char *strr1 = str4;

    //        char *strr2 = str5;

            SUN p4 = sun;

            printf("---===%d ",p4("he","llo"));

            //给函数指针赋值的 函数,应该和函数指针定义的函数原型 保持一致。

            

    //定义两个 函数 ,一个求 最大值,一个 求和,输入maxValue或sum分别求3,5的最大值或和

    //(提示,定义一个 函数指针 ,根据输入内容指向不同函数,最后一次调用完成)。

    //        MAXV p5 = NULL;

    //        char let[10];

    //        printf("输入maxValue或sum: ");

    //        scanf("%s",let);

    //        if (strcmp(let, "maxValue") == 0) {

    //            p5 = maxValue;

    //        } else if (strcmp(let, "sum") == 0) {

    //            p5 = sum;

    //        } else {

    //            printf("null.... ");

    ////            return 0;

    //        }

    //        if (p5 != NULL) {

    //            printf("%d ",p5(60,5));

    //        }

    ////        printf("%d ",p5(60,5));

            

            

            //回调函数callback

            MAXV p6 = NULL;

            p6 = mult;

    //        int h = getValue(3, 5, p6);

            printf("%d ",getValue(3, 5, p6));

            

            

            MAXV p7 = sum;

            printf("%d ",p7(7,9));

            printf("%d ",getValue(6, 6, p7));

            printf("%d ",getValue(6, 6, mult));

            printf("%d ",getValue(6, 6, maxValue));

            

            //动态排序

            

    //        //2.

    //        int (*p)(int x, int y);

    //        p = sum;

    //        int y = p(7, 9);

    //        printf("y = %d ", y);

    //        //3.

    //        int (*p)(int x, int y) = sum;

    //        int y = p(7, 9);

    //        printf("y = %d ", y);

    //        //4.

    //        int (*p)(int, int) = NULL;

    //        p = sum;

    //        int y = p(7, 9);

    //        printf("y = %d ", y);

            

        }

        return 0;

    }

  • 相关阅读:
    用感知机(Perceptron)实现逻辑AND功能的Python3代码
    xpadder教程:自定义设置游戏手柄的图片
    用Python实现小说中的汉字频率统计
    天猫精灵X1智能音箱使用感想
    一道常被人轻视的前端JS面试题
    jQueryNotes仿QQ空间添加标记
    JQ对象到底是什么
    正则匹配规则
    自定义右键菜单
    IIS处理并发请求时出现的问题及解决
  • 原文地址:https://www.cnblogs.com/lzp-1713396133/p/4930936.html
Copyright © 2011-2022 走看看