码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-09-19 22:25:27      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

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);

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

标签:

原文地址:http://www.cnblogs.com/guxuanqing/p/4822359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!