1. 程式人生 > 其它 >C語言 - 指標 1.5:函式指標 | 指標函式

C語言 - 指標 1.5:函式指標 | 指標函式

函式指標

1 - 函式指標是指向函式的指標變數,本質是一個指標變數。宣告格式:int (*maxValue) (int x,) 

2 - 函式指標只能指向具有特定特徵的函式,要求所有被同一指標所指向的函式必須具有相同的引數和返回值型別。比如 void (*func) ( ) 首先執行的是 (*func),func 是一個指標;緊接著執行 ( ),表明 func 指向的是一個函式;void 則標明這個函式不返回任何值

3 - 程式碼示例

① 如何使用函式指標

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 //
 5
int sumValue(int x,int y){ 6 int sum =0; 7 sum = x+y; 8 return sum; 9 } 10 11 // 較大值 12 int maxValue(int a,int b){ 13 return a > b ? a : b; 14 } 15 16 // 遍歷求和 17 int number (int x){ 18 19 int sum=0; 20 for (int i=0; i<x; i++) { 21 sum += i; 22 } 23 return
sum ; 24 } 25 26 // 平方 27 int squareValue (int y){ 28 int square =0; 29 square = y*y; 30 return square; 31 } 32 int main(int argc, const char * argv[]) { 33 34 // 35 int (*p) (int,int)= sumValue;// 函式指標 p 36 printf("%d \n",sumValue(10, 20));// 方式 1 37 printf("%d \n",p(10,20));// 方式 2
38 printf("%d \n",(*p)(10,20));// 方式 3 39 40 printf("%p \n",sumValue); 41 printf("%p \n",p); 42 43 44 // 取較大值 45 p = maxValue;// 函式指標重指向,等價於 p = &maxValue;(取址運算子可以忽略) 46 printf("%d \n",maxValue(10, 20)); 47 printf("%d \n",p(10,20)); 48 printf("%p \n",maxValue); 49 printf("%p \n",p); 50 51 52 // 遍歷求和 53 int (*q) (int ) = number; 54 printf("%d \n",number(101)); 55 printf("%d \n",q(101)); 56 57 // 相加或平方 58 int (*point)(int) = NULL; 59 printf("請輸入sum或者squ:\n"); 60 char string [20] =" "; 61 scanf("%s",string); 62 if (strcmp(string, "sum")==0) { 63 point = number; 64 }else if(strcmp(string, "squ")==0){ 65 66 point = squareValue; 67 } 68 69 printf("%s的結果為:%d\n",string,point(10)); 70 71 return 0; 72 }

注:C語言 規定函式指示符(即函式名)既不是左值也不是右值,但是 CPP語言 規定函式名屬於左值

  函式名除了可以作為 sizeof 和取地址 & 的運算元,函式名在表示式中可以自動轉換為函式指標型別的右值。所以通過一個函式指標呼叫所指向的函式不需要在函式指標面前新增操作符號 *

  指向函式的指標變數沒有自增 ++ 和 自減 – 操作

② 輸入學生資訊(姓名、年齡、分數),以既定分數為標準將學生資訊按照年齡進行升序

 1 #include <stdio.h>
 2 #include <stdbool.h>
 3 #include <stdlib.h>
 4 typedef struct student{
 5 
 6     char  name[20];// 姓名
 7     float score;   // 分數
 8     int   age;     // 年齡
 9 
10 }Student;
11 
12 // 注意這裡是指標函式
13 Student *genterStudentsInfo(int count){
14     // 開闢記憶體,儲存學生資訊
15     Student *stus = malloc(sizeof(Student)*count );
16     for (int i = 0; i < count; i++) {
17         printf("請輸入第%d個學生的資訊:\n",i+1);
18         printf("姓名 成績 年紀:\n");
19         scanf("%s %f %d",(stus+i)->name,&(stus+i)->score,&(stus+i)->age);
20     }
21     return stus;
22 };
23 
24 // 名字後面拼接標記
25 void modifyName(char *name){
26     strcat(name, "---學霸");
27 }
28 void Name(char *name){
29     strcat(name, "---學渣");
30 }
31 
32 // 查詢分數:高於指定分數標記為學霸,低於指定分數標記為學渣
33 void searchStudentInfo(Student *stus,int count,float destScore,void(*p)(char*),void(*q)(char*)){
34 
35     for (int i = 0;i< count ;i++) {
36 
37         if ((stus+i)->score > destScore) {
38             p((stus+i)->name);// 學霸
39         }else{
40             q((stus+i)->name);// 學渣
41         }
42     }
43 }
44 
45 // 升序
46 bool isAscending(int age1,int age2){
47     return age1 > age2 ? 1 : 0;
48 }
49 
50 // 降序
51 bool isDecending(int age1,int age2){
52     return age1 < age2 ? 1 : 0;
53 }
54 
55 bool(*p) (int,int);
56 typedef bool(*Compare) (int,int);
57 
58 // 排序
59 void sortStudentInfo(Student *stus,int count,Compare point){
60 
61     for(int i=0; i<count-1; i++) {
62         for(int j=0; j<count-1-i; j++) {
63 
64             if(point ((stus+j)->age,(stus+j+1)->age)){
65                 Student temp = *(stus+j);
66                 *(stus+j) = stus[j+1];
67                 *(stus+j+1) = temp;
68             }
69         }
70     }
71 }
72 
73 // 列印學生資訊
74 void printStudentInfo(Student *stus,int count){
75     printf("\n----------------------------------\n");
76     for (int i=0; i<count ; i++) {
77         printf("姓名:%s, 成績:%.2f,年齡:%d \n",stus[i].name,(stus+i)->score,stus[i].age);
78     }
79     printf("----------------------------------\n");
80 }
81 
82 int main(int argc, const char * argv[]) {
83 
84     Student *students =  genterStudentsInfo(3);
85     searchStudentInfo(students, 3, 90, modifyName,Name);
86     sortStudentInfo(students, 3, isAscending);// 升序
87     printStudentInfo(students, 3);
88 
89     return 0;
90 }

日誌列印

 

指標函式

1 - 簡單來說,就是一個返回指標的函式,其本質是一個函式,而該函式的返回值是一個指標。宣告格式:型別識別符號 *函式名(引數...);  如:int *fun(int x,int y);