日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 設(shè)計應(yīng)用 > C語言函數(shù)指針的六個高級應(yīng)用場景

          C語言函數(shù)指針的六個高級應(yīng)用場景

          作者: 時間:2023-06-20 來源: 收藏

          函數(shù)指針是一種非常強大的編程工具,它可以讓我們以更加靈活的方式編寫程序。在本文中,我們將介紹 6 個函數(shù)指針的高級應(yīng)用場景,并貼出相應(yīng)的代碼案例和解釋。

          本文引用地址:http://yuyingmama.com.cn/article/202306/447861.htm


          回調(diào)函數(shù)

          回調(diào)函數(shù)是指在某個事件發(fā)生時被調(diào)用的函數(shù)。通常,回調(diào)函數(shù)是在某個庫函數(shù)或框架函數(shù)中注冊的,當(dāng)某個條件滿足時,庫函數(shù)或框架函數(shù)會調(diào)用回調(diào)函數(shù)來執(zhí)行相應(yīng)的操作。以下是一個示例:

          #include

          void handle_event(int event_type, void (*callback)(void))

          {

             printf("event %d occurredn", event_type);

             if (callback)

              {

                 callback();
             }
          }

          void callback_function()

          {

             printf("callback function calledn");
          }

          int main()

          {

             handle_event(1, callback_function);
             handle_event(2, NULL);
             
          return 0;
          }

          在上面的代碼中,我們定義了一個 handle_event 函數(shù),它接受兩個參數(shù):一個事件類型和一個函數(shù)指針。如果函數(shù)指針不為空,則會調(diào)用指定的函數(shù)。

          在 main 函數(shù)中,我們分別調(diào)用 handle_event 函數(shù)來觸發(fā)兩個事件,其中第一個事件注冊了一個回調(diào)函數(shù) callback_function,第二個事件沒有注冊回調(diào)函數(shù)。

          函數(shù)參數(shù)化

          函數(shù)參數(shù)化是指通過函數(shù)指針將函數(shù)的某些行為參數(shù)化。這樣,我們可以在調(diào)用函數(shù)時動態(tài)地指定函數(shù)的行為。以下是一個示例:

          #include
          void process_array(int *array, size_t size, int (*process)(int))

          {
             for (size_t i = 0; i < size; i++)

              {
                 array[i] = process(array[i]);
             }
          }
          int increment(int n)

          {
             return n + 1;
          }
          int main()

          {
             int array[] = {1, 2, 3, 4, 5};
             size_t size = sizeof(array) / sizeof(int);
             process_array(array, size, increment);
             for (size_t i = 0; i < size; i++)

              {
                 printf("%d ", array[i]);
             }
             printf("n");
             return 0;
          }

          在上面的代碼中,我們定義了一個 process_array 函數(shù),它接受三個參數(shù):一個整型數(shù)組、數(shù)組大小和一個函數(shù)指針。函數(shù)指針指向一個函數(shù),該函數(shù)接受一個整型參數(shù)并返回一個整型結(jié)果。

          在 process_array 函數(shù)中,我們將數(shù)組中的每個元素傳遞給指定的函數(shù),然后將函數(shù)的返回值存儲回原數(shù)組中。

          在 main 函數(shù)中,我們定義了一個 increment 函數(shù),它將傳入的整數(shù)加 1。然后,我們調(diào)用 process_array 函數(shù)來處理整型數(shù)組,并打印出結(jié)果。

          排序算法

          排序算法是函數(shù)指針的另一個常見應(yīng)用場景。通過傳遞不同的比較函數(shù),我們可以在不同的排序算法中重用相同的代碼。以下是一個示例:

          #include
          #include
          typedef int (*compare_func_t)(const void *, const void *);
          void sort(int *array, size_t size, compare_func_t compare_func)

          {
             qsort(array, size, sizeof(int), compare_func);
          }
          int compare_int(const void *a, const void *b)

          {
             return (*(int*)a - *(int*)b);
          }
          int compare_reverse_int(const void *a, const void *b)

          {
             return (*(int*)b - *(int*)a);
          }
          int main()

          {
             int array[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
             size_t size = sizeof(array) / sizeof(int);
             sort(array, size, compare_int);
             for (size_t i = 0; i < size; i++)

              {
                 printf("%d ", array[i]);
             }
             printf("n");
             sort(array, size, compare_reverse_int);
             for (size_t i = 0; i < size; i++)

              {
                 printf("%d ", array[i]);
             }
             printf("n");
             return 0;
          }

          在上面的代碼中,我們定義了一個 sort 函數(shù),它接受三個參數(shù):一個整型數(shù)組、數(shù)組大小和一個比較函數(shù)指針。

          比較函數(shù)指針指向一個函數(shù),該函數(shù)接受兩個指向常量 void 類型的指針,并返回一個整型結(jié)果。

          在 sort 函數(shù)中,我們使用標(biāo)準(zhǔn)庫函數(shù) qsort 來對整型數(shù)組進(jìn)行排序,其中比較函數(shù)指針由調(diào)用者傳遞。

          在 main 函數(shù)中,我們定義了兩個比較函數(shù) compare_int 和 compare_reverse_int,分別用于升序和降序排序。然后,我們調(diào)用 sort 函數(shù)來對整型數(shù)組進(jìn)行排序,并打印出結(jié)果。

          函數(shù)指針數(shù)組

          函數(shù)指針數(shù)組是指一個數(shù)組,其中的每個元素都是一個函數(shù)指針。這種數(shù)組可以用于實現(xiàn)一個分派表,根據(jù)輸入?yún)?shù)的不同,動態(tài)地調(diào)用不同的函數(shù)。以下是一個示例:

          #include
          void add(int a, int b)

          {
             printf("%d + %d = %dn", a, b, a + b);
          }
          void subtract(int a, int b)

          {
             printf("%d - %d = %dn", a, b, a - b);
          }
          void multiply(int a, int b)

          {
             printf("%d * %d = %dn", a, b, a * b);
          }
          void divide(int a, int b)

          {
             if (b == 0)

             {
                 printf("cannot divide by zeron");
             }

              else

              {
                 printf("%d / %d = %dn", a, b, a / b);
             }
          }
          typedef void (*operation_func_t)(int, int);
          int main()

          {
             operation_func_t operations[] = {add, subtract, multiply, divide};
             size_t num_operations = sizeof(operations) / sizeof(operation_func_t);
             int a = 10, b = 5;
             for (size_t i = 0; i < num_operations;i++)
             {
              operations[i](a,b);
             }
             return 0;
          }

          在上面的代碼中,我們定義了四個函數(shù) add、subtract、multiply 和 divide,分別對兩個整數(shù)進(jìn)行加、減、乘和除操作。

          然后,我們定義了一個函數(shù)指針類型 operation_func_t,它指向一個接受兩個整型參數(shù)并沒有返回值的函數(shù)。

          接著,我們定義了一個函數(shù)指針數(shù)組 operations,其中的每個元素都是一個 operation_func_t 類型的函數(shù)指針,分別指向 add、subtract、multiply 和 divide 函數(shù)。

          在 main 函數(shù)中,我們使用 for 循環(huán)遍歷 operations 數(shù)組,并依次調(diào)用每個函數(shù)指針?biāo)赶虻暮瘮?shù)。在每次調(diào)用函數(shù)之前,我們可以根據(jù)需要設(shè)置 a 和 b 的值。這樣,我們就可以動態(tài)地選擇要執(zhí)行的操作。

          函數(shù)指針與回溯法

          回溯法是一種求解一些組合優(yōu)化問題的算法,它通常使用遞歸來實現(xiàn)。函數(shù)指針可以用于實現(xiàn)回溯法算法的一些關(guān)鍵部分。

          以下是一個使用回溯法來計算排列的示例:

          #include
          #include
          typedef void (*callback_func_t)(const int *, size_t);
          void swap(int *a, int *b)

          {
             int tmp = *a;
             *a = *b;
             *b = tmp;
          }
          void permute(int *nums, size_t len, size_t depth, callback_func_t callback) {
             if (depth == len)

              {
                 callback(nums, len);
                 return;
             }
             for (size_t i = depth; i < len; i++)

              {
                 swap(&nums[depth], &nums[i]);
                 permute(nums, len, depth + 1, callback);
                 swap(&nums[depth], &nums[i]);
             }
          }
          void print_array(const int *arr, size_t len)
          {
             for (size_t i = 0; i < len; i++)
             {
               printf("%d ", arr[i]); }
               printf("n");
           }
          }
          int main()
          {
           int nums[] = {1, 2, 3};
           permute(nums, sizeof(nums) / sizeof(int), 0, print_array);
           return 0;
          }

          在上面的代碼中,我們定義了一個函數(shù) permute,用于計算給定數(shù)組的排列。

          在 permute 函數(shù)中,我們使用遞歸來生成所有可能的排列,并使用函數(shù)指針 callback 來指定每當(dāng)我們生成一個排列時應(yīng)該調(diào)用的函數(shù)。

          在本例中,我們將 print_array 函數(shù)作為回調(diào)函數(shù)傳遞給了 permute 函數(shù)。這意味著每當(dāng) permute 函數(shù)生成一個排列時,它都會調(diào)用 print_array 函數(shù)來打印這個排列。

          在 main 函數(shù)中,我們定義了一個包含三個整數(shù)的數(shù)組 nums,并使用 permute 函數(shù)來計算這個數(shù)組的所有排列。在每次生成一個排列時,permute 函數(shù)都會調(diào)用 print_array 函數(shù)來打印這個排列。

          函數(shù)指針與多態(tài)

          多態(tài)是面向?qū)ο缶幊讨械囊粋€重要概念,它允許我們在不知道對象類型的情況下調(diào)用相應(yīng)的函數(shù)。雖然 C 語言不是面向?qū)ο缶幊陶Z言,但我們?nèi)匀豢梢允褂煤瘮?shù)指針來實現(xiàn)多態(tài)。

          以下是一個使用函數(shù)指針實現(xiàn)多態(tài)的示例:

          #include
          #include
          typedef struct shape

          {
             void (*draw)(struct shape *);
          } shape_t;
          typedef struct circle

          {
             shape_t shape;
             int x;
             int y;
             int r;
          } circle_t;
          typedef struct rectangle

          {
             shape_t shape;
             int x;
             int y;
             int w;
             int h;
          } rectangle_t;
          void circle_draw(shape_t *shape)

          {
             circle_t *circle = (circle_t *)shape;
             printf("Drawing a circle at (%d, %d) with radius %d.n", circle->x, circle->y, circle->r);
          }
          void rectangle_draw(shape_t *shape)

          {
             rectangle_t *rectangle = (rectangle_t *)shape;
             printf("Drawing a rectangle at (%d, %d) with width %d and height %d.n", rectangle->x, rectangle->y, rectangle->w, rectangle->h);
          }
          int main()

          {
             circle_t circle =

              {
                 .shape = {circle_draw},
                 .x = 10,
                 .y = 20,
                 .r = 5,
             };
             rectangle_t rectangle =

              {
                 .shape = {rectangle_draw},
                 .x = 30,
                 .y = 40,
                 .w = 15,
                 .h = 20,
             };
             shape_t *shapes[] = {(shape_t *)&circle, (shape_t *)&rectangle};
             for (size_t i = 0; i < sizeof(shapes) / sizeof(shape_t *); i++)

              {
                 shapes[i]->draw(shapes[i]);
              }
              return 0;
           }

          在上面的代碼中,我們定義了一個 shape 結(jié)構(gòu)體,它有一個函數(shù)指針 draw,用于繪制該形狀。

          我們還定義了兩個形狀:circle 和 rectangle,它們分別包含它們自己的屬性和一個指向 shape 結(jié)構(gòu)體的指針。每個形狀都定義了自己的 draw 函數(shù),用于繪制該形狀。

          在 main 函數(shù)中,我們定義了一個 shape_t 類型的數(shù)組,其中包含一個 circle 和一個 rectangle。我們使用一個循環(huán)來遍歷這個數(shù)組,并使用每個形狀的 draw 函數(shù)來繪制該形狀。

          注意,盡管 shapes 數(shù)組中的元素類型為 shape_t *,但我們?nèi)匀豢梢哉{(diào)用每個元素的 draw 函數(shù),因為 circle 和 rectangle 都是從 shape_t 派生出來的,它們都包含一個 draw 函數(shù)指針。

          這個例子演示了如何使用函數(shù)指針來實現(xiàn)多態(tài)。盡管 C 語言不支持面向?qū)ο缶幊蹋覀兛梢允褂媒Y(jié)構(gòu)體和函數(shù)指針來實現(xiàn)類似的概念。

          總結(jié)

          函數(shù)指針是一種強大的工具,可以用于實現(xiàn)許多不同的編程模式和算法。

          本文中,我們介紹了函數(shù)指針的基本概念和語法,并提供了一些高級應(yīng)用場景的代碼示例,包括回調(diào)函數(shù)、函數(shù)指針數(shù)組、函數(shù)指針作為參數(shù)、函數(shù)指針與遞歸、函數(shù)指針與多態(tài)等。

          使用函數(shù)指針可以幫助我們編寫更加靈活和通用的代碼,并提高代碼的可重用性和可擴展性。

          原文:https://zhuanlan.zhihu.com/p/625882785
          文章來源于網(wǎng)絡(luò),版權(quán)歸原作者所有,如有侵權(quán),請聯(lián)系刪除。


          關(guān)鍵詞:

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉