指针函数   函数指…

指针函数:是函数,但是返回值是一个指针。

函数指针:是指针,但是指向一个函数的入口地址。
函数指针数组:是数组,数组里的元素为指针,而指针指向函数入口地址。

【1】

#include

int test(int a,int b,int(*pFunc)(int m,int n));
int Plus(int a,int b);
int Minus(int,int);


int main()
{
 int x=5,y=8;
 int(*pFunc)(int a,int b);//为函数指针
 pFunc=Plus;              //指向Plus
 printf("%d\n",(*pFunc)(x,y));   //输出Plus
 pFunc=Minus;
 printf("%d\n",(*pFunc)(x,y));
 printf("%d\n",test(15,5,Plus));//函数指针作为函数的变量,传递时传的是函数名
 printf("%d\n",test(15,5,Minus));
 return 0;
}

int Plus(int a,int b)
{
 return (a+b);
}
int Minus(int a,int b)
{
 return (a-b);
}

int test(int a,int b,int(*pFunc)(int m,int n))
{
 return ((*pFunc)(a,b));
}

【2】

#include
int Plus(int a,int b);
int Minus(int a,int b);
int main()
{
 int (*pFunc[2])(int a,int b);//函数指针数组
 int i;
 pFunc[0]=Plus;
 pFunc[1]=Minus;              //分别指向不同的函数
 for(i=0;i<=1;i++)
 printf("%d\n",(*pFunc[i])(15,25));   //通过指针调用函数
}
int Plus(int a,int b)
{return (a+b);}

int Minus(int a,int b)
{return (a-b);}


【3】指针函数多用于处理字符串问题(输入字符串将其中的空格删去再输出如“abc def hi”->"abcdefhi")

#include
#include
char *delete(char *p)          //指针函数返回指针
{
 
 char b[100] = {};
 int i=0,len=0,j=0;
 while(*(p+i) != '\0')
 {
  if(*(p+i) != ' ')
  {
   b[j]=*(p+i);
   j++;
  }
  i++;

 }
 return (b);          返回指针
}

int main()
{
 char a[100];
 gets(a);
 printf("%s\n",a);
 printf("%s\n",delete(a)); 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值