现在的位置: 首页 > 综合 > 正文

指向函数的指针

2012年08月07日 ⁄ 综合 ⁄ 共 1128字 ⁄ 字号 评论关闭
#include "stdio.h"
#include 
"conio.h"
#define M 8
float max(float a[],int n);    /*函数声明*/
void main()
{
  clrscr();
  
float sumf,sump;
  
float a[M]={11,2,-3,4.5,5,69,7,80};
  
float (*p)(float a[],int);   /*定义指向函数的指针 p*/
  p
=max;                       /*函数名(函数入口地址)赋给指针p*/
  sump
=(*p)(a,M);              /*用指针方式调用函数*/
  sumf
=max(a,M);               /*用函数名调用max()函数*/
  printf(
"sump=%.2f\n",sump);
  printf(
"sumf=%.2f\n",sumf);
}


float max(float a[],int n)
{
  
int k;
  
float s;
  s
=a[0];
  
for(k=0;k<n;k++)
    
if(s<a[k]) s=a[k];
  
return s;
}

在主函数中输入三角形的两条直角边,求三角形的面积、斜边长

#include "stdio.h"
#include 
"conio.h"
#include 
"math.h"
float area(int,int);
float length(int,int);
float f(int,int,float (*p)(int,int));
void main()
{
  clrscr();
  
int m,n;
  
float s,l;
  
float (*q)(int,int);
  scanf(
"%d,%d",&m,&n);
  q
=area;
  s
=f(m,n,q);
  l
=f(m,n,length);
  printf(
"Area of the right triangle is %.2f\n",s);
  printf(
"Length of the hypotenuse is %.2f\n",l);
}


float area(int a,int b)
{
  
float z;
  z
=a*b/2;
  
return z;
}


float length(int a,int b)
{
  
float z;
  z
=sqrt(a*a+b*b);
  
return z;
}


float f(int a,int b,float (*p)(int,int))
{
  
float y;
  y
=(*p)(a,b);
  
return y;
}

抱歉!评论已关闭.