1. 函式指標
函式名 VS &函式名
對于陣列而言,陣列名=首元素地址,&陣列名=整個陣列的地址
那么函式名和&函式名等于什么
#include <stdio.h>
void test()
{
;
}
int main()
{
test();
printf("%p\n", test);
printf("%p\n", &test);
}
注:函式名和&函式名不能+-整數

結論是:函式名和&函式名一樣 = 函式的地址
什么是函式指標
既然函式名=函式地址,就可以用一個變數進行存盤
這個變數就是函式指標,存盤一個函式地址的變數
#include <stdio.h>
int test(int x, int y)
{
;
}
int main()
{
int(*ptr)(int, int) = test;
}
這里的ptr就是一個函式指標
(*ptr),表示ptr是一個指標變數 ,(int, int), 表示ptr存盤的是一個函式的地址,這個函式有兩個int型別的引數,(*ptr)前面的int, 表示這個函式的回傳型別為int整形
函式指標的應用
#include <stdio.h>
int test(int x, int y)
{
return x + y;
}
int main()
{
int(*ptr)(int, int) = test;
int ret = (*ptr)(1,2);
printf("%d\n", ret);
}

*ptr表示通過地址訪問空間 ---> 得到函式的地址 ---> 傳值(1,2) ---> 得到結果3 ---> 列印
函式指標作為引數
// 使用冒泡排序,排序任意型別陣列
#include <string.h>
#include <stdio.h>
struct Stu
{
char name[10];
int age;
};
int cmp_int(const void* e1, const void* e2)
{
return *(int*)e1 - *(int*)e2;
}
// 以年齡比較
int cmp_by_name(const void* e1,const void* e2)
{
return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}
// 以名字比較
int cmp_by_age(const void* e1, const void* e2)
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
// 交換
void swap(char* buf1, char* buf2, int width)
{
while (width--)
{
char tmp = *buf1;
*buf1 = *buf2;
*buf2 = tmp;
buf1++;
buf2++;
}
}
// 排序
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* e1, const void* e2))
{
int i = 0;
for (i = 0; i < sz - 1; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if ((*cmp)( (char*)base+(j*width), (char*)base+((j+1)*width)) > 0)
{
swap((char*)base + (j * width), (char*)base + ((j + 1) * width), width);
}
}
}
}
//排序整形陣列
void sort_int()
{
int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
}
// 排序結構體陣列
void sort_struct()
{
struct Stu t1[5] = { {"c",5 }, { "a",4 }, { "b",3 }, { "e",2 }, { "d",1 } };
int sz = sizeof(t1) / sizeof(t1[0]);
bubble_sort(t1, sz, sizeof(t1[0]), cmp_by_name); // 以名字排序
//bubble_sort(t1, sz, sizeof(t1[0]), cmp_by_age);// 以年齡排序
}
int main()
{
//sort_int();
sort_struct();
}
2. 函式指標陣列
什么是函式指標陣列
首先梳理一下概念,陣列是一組相同型別元素的集合,函式指標是存盤函式地址的變數
所以函式指標陣列,就是存盤一組函式地址的集合
#include <stdio.h>
void test1()
{
;
}
void test2()
{
;
}
void test3()
{
;
}
int main()
{
void(*ptr[3])() = { test1,test2,test3 };
}
void (* ptr[3] )() , 如何理解 ?
ptr首先會與[3]結合,表示ptr是一個陣列,陣列有3個元素
void (* ptr[3] )() ---> void (*)(),
(*) 表示陣列的每一個元素是一個指標,()表示這個指標是一個函式指標且函式沒有引數,void表示函式回傳值為空
函式指標陣列的應用
#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
void menu()
{
printf("1.add 2.sub\n");
printf("3.mul 4.div\n");
}
int main()
{
int(*ptr[5])(int, int) = { 0,add,sub,mul,div }; //函式指標陣列
int input = 0;
int x = 0;
int y = 0;
int ret = 0;
do
{
menu();
printf("輸入: ");
scanf("%d", &input);
if (input > 4 || input <= 0)
{
printf("退出程式\n");
}
else
{
printf("輸入x: ");
scanf("%d", &x);
printf("輸入y: ");
scanf("%d", &y);
ret = (ptr[input])(x, y); //通過input,鎖定函式地址,最后傳參得到結果
printf("%d\n", ret);
}
} while (input);
}
3. 指向函式指標陣列的指標
直接看代碼
#include <stdio.h>
int main()
{
int arr[10] = { 0 }; // 整形陣列
int(*pa)[10] = &arr; // 陣列指標,指向陣列的指標
int(*pf[10])(int, int); // 函式指標陣列
int(*(*ppf)[10])(int,int)= &pf // 指向函式指標陣列的地址
}
既然可以用&運算子取出一個整形陣列的地址,那么也可以取出一個函式指標陣列的地址
所以,指向函式指標陣列的指標就是存盤整個函式指標陣列的地址的變數
int (* (*ppf) [10] ) (int, int) 如何理解 ?
(*ppf), 表示ppf是一個指標, [10]表示這個指標存盤一個陣列的地址,陣列有10個元素,int (*) (int, int), 表示陣列的每一個元素是一個函式指標
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/544394.html
標籤:C
上一篇:閱讀openfoam框圖
