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:\n", m);
19         p = search(score, m);
20         for (int i = 0; i < NOCLASS; ++i) {
21             //請思考Line22和Line35為什么不一樣呢, 一維與二維
22             printf("%5.2f\t", *(p+i));
23             printf("p+i:%#x\n",p+i);
24         }
25         printf("\n");
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\n", pointer);
35     pt = *(pointer + iter);//*(*(p+iter)+0)) = *(*(p+iter))
36     printf("pt:%#x\n", 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\n", 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", n);
 8     return n;  
 9 }
10 int func2(int n)
11 {
12     printf("func2: %d\n", 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];

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM