//

//  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. Qt对话框QDialog

    QDialog是Qt中所有对话框窗口的基类 当QWidget无父组件的时候作为一个独立的窗口,有父组件的时候,将作为一个可见的部件嵌入到父组件里面. QDialog不能作为子部件嵌入到其他容器中 对话 ...

  2. 一个表的两个列连接另外一个表的一个列SQL语句怎么写

    f619424517 | 浏览 2207 次 推荐于2016-09-09 11:38:18   最佳答案   select a.flightid,a.flightname,b.cityname,c.c ...

  3. Eclipse Code Template 设置自动加注释(转)

    Eclipse Code Template 设置自动加注释 设置注释模板的入口: Window->Preference->Java->Code Style->Code Temp ...

  4. String的成员方法的使用

    <%@ page language="java" contentType="text/html; charset=gbk"%> <html&g ...

  5. C++程序在debug模式下遇到Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call问题。

    今天遇到一个Access Violation的crash,只看crash call stack没有找到更多的线索,于是在debug模式下又跑了一遍,遇到了如下的一个debug的错误提示框: 这个是什么 ...

  6. MapObject shape数据操作

    利用MO 的GeoDataset  .DataConnection  Recordset 进行数据操作 在MO中.使用GeoDataset对象和DataConnection 对象来连接和读取地理数据. ...

  7. [RxJS] Transformation operator: map and mapTo

    We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...

  8. spring mvc DispatcherServlet详解之拾忆工具类utils

    DispatcherServlet的静态初始化 /** * Name of the class path resource (relative to the DispatcherServlet cla ...

  9. 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高

    第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...

  10. css 权威指南笔记(二)元素

    替换元素 用来替换元素内容的部分并非有文档内容直接表示. img input 非替换元素 其内容由用户代理(通常是一个浏览器)在元素本身生成的框中显示. 块级元素 块级元素生成一个 元素框,(默认)会 ...