学习指针型函数和指向函数的指针

#include<stdlib.h>
#include<string>
#include<math.h>
#include<iostream>
using namespace std;
char *str(char *a , char *b);
int main()
{
    char str1[] = "Hello ";
    char str2[] = "Welcome C++";
    char *max = 0;

    cout<<str(str1 , str2)<<endl;
//  int m = strlen(str1) - strlen(str2);
//  cout<<max<<(strlen(str1) - strlen(str2))<<endl;         //为什么此处相减为正数,


        //因为原型strlen()返回的是一个无符号类型的数,不能表示负数!!
    system("pause");
}

char *str(char *a , char *b)
{
    return ( strlen(a) > strlen(b)  ?  a : b);      //返回的为较大的字符串


    // ?前边若为非零,计算值为 a,反之为 b;
}

/*总结:指针函数
    数据类型 *函数名(参数表···) 
    1.其中数据类型为返回的指针所指向的数据类型!
    2.指针型函数返回的为一个指针(地址)!!*/

*/

//******************************(8.03-10-05)**指向函数的指针***************************
/*
#include<stdlib.h>
#include<string>
#include<math.h>
#include<iostream>
int max(int a , int b);
using namespace std;
int main()
{
    int (*p)(int a , int b);        //定义了一个指针指向函数,以后再用到这个函数

的时候可以调用这个指针!
    p = max;
    cout<<(*p)(12 , 465)<<endl;

    system("pause");
}

int max(int a , int b)
{
    return ( (a > b) ? a : b );
}

/*总结:
    1.注意函数指针的定义形式:
        int (*p)(int a , int b)  (*指针名)要用()括号括起来!,这是与返回值为指针的函

数唯一不同的地方,但是本质却一点不同!*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值