c語言之函數指標的一系列測試,函數指標一系列
1.先從一個簡單的求和求積函數開始
#include <stdio.h>int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int main(){ int a_count = add(5,7); int m_count = mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0;}
輸出:
a_count is 12
m_count is 35
Program ended with exit code: 0
2.試一下函數指標
#include <stdio.h>int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int main(){ int (*p_add)(int,int);//聲明函數指標 p_add = add; int (*p_mul)(int,int); p_mul = mul; int a_count = p_add(5,7); int m_count = p_mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0;}
結果不變
a_count is 12
m_count is 35
Program ended with exit code: 0
3.簡化一下函數指標的定義
#include <stdio.h>int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int main(){ typedef int (*fun)(int,int);//定義類型fun是一個指向函數的指標 fun p_add = add; fun p_mul = mul; int a_count = p_add(5,7); int m_count = p_mul(5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0;}
結果不變
a_count is 12
m_count is 35
Program ended with exit code: 0
4.嘗試使用一下函數指標數組
#include <stdio.h>int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int main(){ int (*func[])(int,int) = {add,mul};//定義函數指標數組 int a_count = func[0](5,7); int m_count = func[1](5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0;}
結果不變
a_count is 12
m_count is 35
Program ended with exit code: 0
5.改善函數指標數組
畢竟,函數多了之後,誰能記住乘法是在數組裡第幾個,下標該是多少。
#include <stdio.h>int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int main(){ int (*func[])(int,int) = {add,mul};//定義函數指標數組 enum func_tpye{ ADD , MUL }; //順序與上面保持一致 int a_count = func[ADD](5,7); int m_count = func[MUL](5,7); printf("a_count is %d\n",a_count); printf("m_count is %d\n",m_count); return 0;}
結果不變
a_count is 12
m_count is 35
Program ended with exit code: 0
6.在函數中調不同的函數
之前的例子,直接用函數會更好用一些
#include <stdio.h>typedef int (*fun)(int,int);int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int add_mul(fun f,int a,int b){ return f(a,b);}int main(){ int (*func[])(int,int) = {add,mul};//定義函數指標數組 enum func_tpye{ ADD , MUL }; //順序與上面保持一致 printf("a_count is %d\n",add_mul(func[ADD], 5, 7)); printf("m_count is %d\n",add_mul(func[MUL], 5, 7)); return 0;}
結果不變
a_count is 12
m_count is 35
Program ended with exit code: 0
7.上面那個例子還可以再擴充的更強大一些
比如說把兩個數相乘相加改為多個數相乘相加
#include <stdio.h>#include <stdarg.h>typedef int (*fun)(int,int);int add(int a , int b){ return a + b;}int mul(int a , int b){ return a * b;}int add_mul(fun f,int n,...){ va_list ap; va_start(ap,n); int count = va_arg(ap, int); for (int i = 0; i<(n-1); ++i) { count = f(count,va_arg(ap, int)); } return count;}int main(){ int (*func[])(int,int) = {add,mul};//定義函數指標數組 enum func_tpye{ ADD , MUL }; //順序與上面保持一致 printf("a_count is %d\n",add_mul(func[ADD], 5,1,2,3,4,5)); printf("a_count is %d\n",add_mul(func[ADD], 3,7,3,6)); printf("m_count is %d\n",add_mul(func[MUL], 5,1,2,3,4,5)); printf("m_count is %d\n",add_mul(func[MUL], 3,7,3,6)); return 0;}
第一個參數為功能函數,第二個參數為運算的個數,之後是不定參數。
結果為:
a_count is 15
a_count is 16
m_count is 120
m_count is 126
Program ended with exit code: 0
C語言怎讓一個函數返回指標
這種方式叫做“用指標函數*creat()的傳回值來傳遞動態記憶體”,這是一個C文法
首先你要注意,子函數*creat()用了malloc動態申請記憶體,而return返回的是指標變數所指向的地址,而不是指標!相當於將你所申請的動態記憶體返回給主函數
int *fun(void)//這裡是指標函數
{
int *p = (int *) malloc (int);//動態申請記憶體//p在這裡指向了一塊合法記憶體的地址
return p;
}
void main(void)
{
int *q = NULL;
q = fun();//fun()返回指標p所指向的地址,(指標p是在棧上建立的,所以*fun()函數運行完,指標所分配的記憶體被系統自動釋放)這時q指向p,得到了malloc所申請到的記憶體
free(q);//釋放動態記憶體
}
C語言怎聲明一個返回函數指標的函數?
typedef int (*AA)();
AA function();