//

//  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\n",str);

}

void printhello(char *str){

printf("大家好,我叫%s\n",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\n",sum(123, 456));

printf("%p\n",&sum);

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

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

//        char stra = 'h';

//        char *strb =&stra;

printf("这是一个字符:%c\n",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\n",p(7,9));

//函数指针重指向

p = mult;

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

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

//        p3 = maxValue;

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

MAXV p3 = maxValue;

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

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

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

//        char *strr1 = str4;

//        char *strr2 = str5;

SUN p4 = sun;

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

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

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

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

//        MAXV p5 = NULL;

//        char let[10];

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

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

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

//            p5 = maxValue;

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

//            p5 = sum;

//        } else {

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

////            return 0;

//        }

//        if (p5 != NULL) {

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

//        }

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

//回调函数callback

MAXV p6 = NULL;

p6 = mult;

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

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

MAXV p7 = sum;

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

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

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

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

//动态排序

//        //2.

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

//        p = sum;

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

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

//        //3.

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

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

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

//        //4.

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

//        p = sum;

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

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

}

return 0;

}

day11基础代码——函数指针的更多相关文章

  1. 嵌入式-C语言基础:函数指针

    定义函数地址:如果在程序中定义了一个函数,那么在编译的时候,编译系统为函数代码分配一段存储空间,这段存储空间的起始地址(也叫入口地址)称为这个函数的地址. 和数组一样,数组名代表地址,而函数名表示函数 ...

  2. C++学习基础十七-- 函数指针

    C++常用的函数指针 语法:返回值类型 (*函数名)(参数列表); 举例说明:int (*Func)(int m, int n); 用typedef简化函数指针的定义 例如: typedef int ...

  3. 成员函数指针与高效C++委托 (delegate)

    下载实例源代码 - 18.5 Kb 下载开发包库文件 - 18.6 Kb 概要 很遗憾, C++ 标准中没能提供面向对象的函数指针. 面向对象的函数指针也被称为闭包(closures) 或委托(del ...

  4. C++基础——函数指针 函数指针数组

    ==================================声明================================== 本文版权归作者所有. 本文原创,转载必须在正文中显要地注明 ...

  5. C语言函数指针基础

    本文写的非常详细,因为我想为初学者建立一个意识模型,来帮助他们理解函数指针的语法和基础.如果你不讨厌事无巨细,请尽情阅读吧. 函数指针虽然在语法上让人有些迷惑,但不失为一种有趣而强大的工具.本文将从C ...

  6. 小猪猪C++笔记基础篇(六)参数传递、函数重载、函数指针、调试帮助

    小猪猪C++笔记基础篇(六) ————参数传递.函数重载.函数指针.调试帮助 关键词:参数传递.函数重载.函数指针.调试帮助 因为一些事情以及自己的懒惰,大概有一个星期没有继续读书了,已经不行了,赶紧 ...

  7. 你必须知道的指针基础-7.void指针与函数指针

    一.不能动的“地址”—void指针 1.1 void指针初探 void *表示一个“不知道类型”的指针,也就不知道从这个指针地址开始多少字节为一个数据.和用int表示指针异曲同工,只是更明确是“指针” ...

  8. C基础--函数指针的使用

    之前在看代码的时候,看了函数指针的使用,大体分为如下几类: 做一个function list,通过指针索引调用,使得处理功能类似的函数看起来更加清晰: 函数指针作为另一个函数的参数,用作回调: lin ...

  9. 编程算法 - 求1+2+...+n(函数指针) 代码(C++)

    求1+2+...+n(函数指针) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...

随机推荐

  1. thread.wait的一个好例子

    int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); HelloThread thread; thread.star ...

  2. Windows 10 Technical Preview 10041 使用 IIS Express 运行网站应用程序异常

    在 Windows 10 中使用 Visual Studio 2013 Ultimate with Update 4 开发网站,9926 的时候还好好的,升到 10041 就不能调试了: “Syste ...

  3. Linux查看系统资源占用

    Linux查看系统资源占用 在系统维护的过程中,随时可能有需要查看 CPU和内存的使用率,并根据相应信息分析系统状况的需求.本文介绍一下几种常见的Linux系统资源查看命令. 1.总体内存占用的查看 ...

  4. java websockect

    https://github.com/TooTallNate/Java-WebSocket (websockect类库包) http://blog.openlg.net/index.php/archi ...

  5. [RxJS] Transformation operator: scan

    All of the combination operators take two or more observables as input. These operators may also be ...

  6. String是java中的基本数据类型吗

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. Ja ...

  7. android 18 Bundle类

    Bundle类:竖屏的activity换到横屏的activity的时候,会把竖屏的activity杀掉横屏的activity创建,竖屏的activity会有一些计算结果,可以用数据存起来,存到内存里面 ...

  8. percona-MYSQLGUI监控

    1.https://www.percona.com/blog/2016/05/26/monitoring-with-percona-app-for-grafana/ http://pmmdemo.pe ...

  9. linux下的僵尸进程处理SIGCHLD信号

    什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打开的文件等,但内核为每一个终止子进程保存了一定量的信息.这些信息至少包括进程ID,进程的终止状态,以及该 ...

  10. phpstorm 快捷方式 (备用)

    常用快捷键  设置快捷键:File -> Settings -> IDE Settings -> Keymap -> 选择“eclipse” -> 然后“Copy”一份 ...