Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335644
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38
文章分类

全部博文(92)

文章存档

2010年(71)

2009年(21)

我的朋友

分类: 嵌入式

2010-06-12 09:51:42

int *p()int (*p)()的区别

int *p()是返回指针的函数

int (*p)()是指向函数的指针

 

返回指针的函数:

int *a(int x,int y);

有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号以后,能输出该学生的全部成绩。用指针函数来实现。

#include

void  main()

float *score ][4={{60708090}

                    {56896788}{34789066}};

   float?*searchfloat (*pointer)4],int n);

   float?*p;

   int i,m;

   printf(″enter the number of  student:″);

   scanf(″%d″,&m);

   printf(″The scores of  No.%d are\n″,m);  

p=search(score,m);

 for(i=0;i<4;i++

 printf(″%5.2f\t″,*(p+i));

float * searchfloat (*pointer)4],int n)

float *pt;

    pt=*(pointer+n);       //(相当于**(p+n)+0))=*(*(p+n))

     return(pt);

 

 

对上例中的学生,找出其中有不及格课程的学生及其学生号。

#include

void  main()

{float *score ][4={{60708090}{56

                          896788}{34789066}};

   float searchfloat (*pointer)4]);

   float?*p;

    int i,j;   

for(i=0;i<3;i++)

 {p=searchscore +i);

  if(p==*score+i))

  {printf(″No.%d scores:″,i);

         for(j=0;j<4;j++)

      printf(″%5.2f″,*(p+j));

         printf(″\n″);}

    

    

 

 

指向函数的指针

实参函数名      f1           f2

                          

void sub(int (*x1)(int)int (*x2)(int,int)

  int a,b,i,j;

     a=*x1)(i); *调用f1函数*

     b=*x2)(i,j);/*调用f2函数*

     

  

例:设一个函数process,在调用它的时候,每次实现不同的功能。输入a和b两个数,第一次调用process时找出a和b中大者,第二次找出其中小者,第三次求a与b之和。

#include

void main()

   int maxintint;            /* 函数声明 */

     int minintint;              /* 函数声明 */

     int addintint);              /* 函数声明 */

     void process (int , int , int(*fun)();    /* 函数声明 */

    int a,b;

    printf(″enter a and b:″);

    scanf(″%d,%d″,&a,&b);

printf(″max=″);

    process(a,b,max);

     printf(″min=″);

     process(a,b,min);

     printf(″sum=″);

     process(a,b,add);

……

int  add(int x,int y)           /* 函数定义 */

  intz;

     z=x+y;

     return(z);

void  process(int x,int y,int (*fun)(int,int))

int  result

   result=(*fun)(x,y);

   printf(″%d\n″, result);

 

阅读(4075) | 评论(0) | 转发(0) |
0

上一篇:二维数组指针

下一篇:带参数的函数

给主人留下些什么吧!~~