指針函數與函數指針

學數據結構用到了指針函數,查閱資料作個總結.

1.指針函數

顧名思義,就是一個帶有指針的函數,本質是一個函數。返回值爲某一類型的指針。

舉例:

#include <stdio.h>
 float *find(float(*pionter)[4],int n);//函數聲明
 int main(void)
 {
     static float score[][4]={{60,70,80,90},{56,89,34,45},{34,23,56,45}};
     float *p;
     int i,m;
     printf("Enter the number to be found:");
     scanf("%d",&m);
     printf("the score of NO.%d are:\n",m);
     p=find(score,m-1);
     for(i=0;i<4;i++)
         printf("%5.2f\t",*(p+i));
  
     return 0;
 }
 
float *find(float(*pionter)[4],int n)/*定義指針函數*/
 {
     float *pt;
     pt=*(pionter+n);
     return(pt);
 }

find 返回了一個指針

2.函數指針即爲指向函數的指針變量,其本質爲指針。

舉例:

#include<stdio.h>
int max(int x,int y){return (x>y? x:y);}
int main()
{
    int (*ptr)(int, int);
    int a, b, c;
    ptr = max;
    scanf("%d%d", &a, &b);
    c = (*ptr)(a,b);
    printf("a=%d, b=%d, max=%d", a, b, c);
    return 0;
}
此例利用了指針來接收返回值。
發佈了46 篇原創文章 · 獲贊 22 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章