zoukankan      html  css  js  c++  java
  • int *p()与int (*p)()的区别

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

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

     

    返回指针的函数:

    int *a(int x,int y);

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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <vector>
     4 #include <string.h>
     5 using namespace std;
     6 #define NOCLASS 4
     7 float *search(float (*pointer)[NOCLASS], int iter);
     8 int main(int argc, char* argv[])
     9 {
    10     float score[][NOCLASS] = {{60,70,80,90}, {56,90,87,67}, {56,89,67,88}, {34,78,90,66},{34,78,90,98},{34,78,90,100}};
    11 
    12     float *p;
    13     int m;
    14     while(1)
    15     {
    16         printf("enter the number of student:");
    17         scanf("%d", &m);
    18         printf("the scores of No.%d are:
    ", m);
    19         p = search(score, m);
    20         for (int i = 0; i < NOCLASS; ++i) {
    21             //请思考Line22和Line35为什么不一样呢, 一维与二维
    22             printf("%5.2f	", *(p+i));
    23             printf("p+i:%#x
    ",p+i);
    24         }
    25         printf("
    ");
    26     }
    27     return 0;
    28 }
    29 
    30 //数组指针,是一个指针,指向一个数组,相当于二位数组,n*4,int,int,int,int
    31 float *search(float (*pointer)[NOCLASS], int iter)
    32 {
    33     float *pt;
    34     printf("pointer:%#x
    ", pointer);
    35     pt = *(pointer + iter);//*(*(p+iter)+0)) = *(*(p+iter))
    36     printf("pt:%#x
    ", pt);
    37     return (pt);
    38 }
    View Code



    指向函数的指针

    实参函数名      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之和。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <vector>
     4 #include <string.h>
     5 using namespace std;
     6 void process(int, int, int (*func)(int, int));
     7 int max(int x, int y);
     8 int min(int, int);
     9 int add(int, int);
    10 int main(int argc, char* argv[])
    11 {
    12   int x = 9, y = 10;
    13   printf("max is:");
    14   process(x, y, max);
    15   printf("min is:");
    16   process(x,y, min);
    17   printf("add is:");
    18   process(x, y, add);
    19     return 0;
    20 }
    21 void process(int x, int y, int (*func)(int, int))
    22 {
    23   int result;
    24   result = (*func)(x,y);
    25   printf("%d
    ", result);
    26 }
    27 int max(int x, int y){
    28   return (x>y?x:y);
    29 }
    30 int min(int x, int y){
    31   return (x<y?x:y);
    32 }
    33 int add(int a, int b){
    34   return (a+b);
    35 }
    View Code

     2016-10-20 19:40:43

    int (*a[10]) (int);这个是定义了一个函数指针数组,指向的函数类型是int func(int)

     1 #include <iostream>
     2 #include <stdio.h>
     3 using namespace std;
     4 
     5 int func1(int n)
     6 {
     7     printf("func1: %d
    ", n);
     8     return n;  
     9 }
    10 int func2(int n)
    11 {
    12     printf("func2: %d
    ", n);
    13     return n;  
    14 }
    15 int main()
    16 {
    17     int (*a[10])(int) = {NULL};
    18     a[0] = func1;
    19     a[1] = func2;
    20     a[0](1);
    21     a[1](2);  
    22     return 0;    
    23 }

    谁见过这种,某面试者出的,感觉糊弄人的,void (*(*func))(int)[10];

     

  • 相关阅读:
    Saltstack 命令参数整理
    Saltstack 命令行:批量覆盖指定文件
    Nginx + Tomcat Windows下的负载均衡配置
    linux和windows同步数据 cwrsync client to rsync server
    Amoeba for MySQL 非常好用的mysql集群软件
    Ubuntu 下 JDK+Tomcat+MySql 环境的搭建
    Ubuntu server下安装JDK和Tomcat7
    EhCache 分布式缓存/缓存集群
    电商系统中的商品模型的分析与设计
    大型网站架构的演化[转]
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/4822359.html
Copyright © 2011-2022 走看看