//

//  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. POJ3186:Treats for the Cows(区间DP)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  2. linux创建用户,指定组

    本博客不再更新 该文章新链接移步:http://it.lovepet.vip/archives/7/ 一.创建用户: 1.使用命令 useradd 例:useradd test——创建用户test  ...

  3. uva10622 Perfect P-th Powers

    留坑(p.343) 完全不知道哪里有问题qwq 从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊 #include<cstdio> #include<cstring&g ...

  4. C#和java和android中的NetWorkAdapter,httpRequest,WebView,json,xml

    原文地址:http://blog.csdn.net/intbird C#NetWorkAdapter 20121011.======================================== ...

  5. android软键盘的用法总结

    1.软键盘的显示原理 软键盘其实是一个Dialog.InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部 或者全屏显示.当我们点击输入框时 ...

  6. java web项目中的web.xml标签之context-param

    WEB项目初始化过程: 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>. 接着容器会创建一 ...

  7. java io 流基础

  8. angular应用前景

    完成了angularJs的学习,突然想到,angularJS是否会影响到seo.于是查阅了很多资料,技术博客,这种想法得到了证实. 爬虫不能识别js渲染的内容.所以引起了我对angular应用前景的思 ...

  9. oracle数据库读取操作系统的物理文件-转载,待完善

    --源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...

  10. 玩转CSLA.NET小技巧系列一:跳转页面丢失session,如何解决

    很少写代码,最近在写代码被登录难倒了,这丫的一直在跟我较劲 每次登录完跳转到首页后还是未登录状态 if (ModelState.IsValid) { bool isSuccess = FI.Finan ...