zoukankan      html  css  js  c++  java
  • 函数指针用法

     示例一:

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 typedef  void *(*func_t)(void*, ...) ;
     5 func_t func;
     6 
     7 typedef struct set {
     8     func_t func;
     9     void * arg;
    10 } set_t;
    11 
    12 
    13 
    14 void func1(int a)
    15 {
    16     printf("func----1	[%d]
    ", a);
    17 }
    18 
    19 int func2(void)
    20 {
    21     printf("func----2
    ");
    22 
    23     return 0;
    24 }
    25 
    26 
    27 char* func3(int a)
    28 {
    29     while (a > 0)
    30         printf("func----3	[%d]
    ", a--);
    31 
    32     return "123";
    33 }
    34 
    35 
    36 int func4(int a, int b)
    37 {
    38 
    39     printf("func----4	[%d] %s [%d]
    ", a, ((a == b) ? "==" : ((a > b) ? ">" : "<")), b);
    40 
    41     return (a + b);
    42 }
    43 
    44 void func5(int a, char* str, int b, float c)
    45 {
    46     c = 9.9;
    47     printf("func----5	[%d] "%s" [%d] [%f]
    ", a, str, b, c);
    48 
    49 }
    50 
    51 void main()
    52 {
    53     set_t set_1;
    54     int a = 3, b = 7;
    55     int *intp;
    56 
    57     func = (void*)func1;
    58     set_1.func = (void*)func1;
    59 
    60     (func)((void*)1);
    61     (set_1.func)((void*)2);
    62     printf("
    
    ");
    63 
    64 
    65     func = (void*)func2;
    66     set_1.func = (void*)func2;
    67 
    68     printf("func2 return: %d
    ", (func)((void*)1));
    69     printf("func2 return: %d
    ", (set_1.func)((void*)2));
    70     printf("
    
    ");
    71 
    72 
    73     func = (void*)func3;
    74     set_1.func = (void*)func3;
    75 
    76     printf("func2 return: %s
    ", (func)((void*)1));
    77     printf("func2 return: %s
    ", (set_1.func)((void*)2));
    78 
    79 
    80     func = (void*)func4;
    81     set_1.func = (void*)func4;
    82     printf("
    
    ");
    83 
    84     printf("func4 return: %d
    ", (func)((void*)9, (void*)50));
    85 
    86     printf("func4 return: %d
    ", (set_1.func)((void *)8, 3));
    87     printf("
    
    ");
    88 
    89 
    90     func = (void*)func5;
    91     set_1.func = (void*)func5;
    92 
    93     (func)((void*)6, "hello world" ,66, (void*)6);
    94     (set_1.func)((void*)5, "hi", 55, (void*)5);
    95     printf("
    
    ");
    96 
    97     intp= (int*)func4;
    98     printf("func4 return: %d
    ", ((func_t)intp)((void *)8, 3));
    99 }

    运行结果:

     

    示例二:

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include <time.h>
     4 
     5 typedef  void *(*func_t)(void*, ...) ;
     6 func_t function;
     7 
     8 typedef struct set {
     9     func_t func;
    10     int start_time;
    11     int wait_time;
    12 } set_t;
    13 
    14 int ex_time(int start, int timer)
    15 {
    16     if ((clock() - start) > timer)
    17         return 1;
    18     else
    19         return 0;
    20 }
    21 
    22 
    23 #define CONNE(x,y)    (x##y)
    24 #define SET_INIT(name) set_t name##set = {
    25                             .func = (void*)name,
    26                             .start_time = 0,
    27                             .wait_time = 0
    28                         };
    29 
    30 
    31 #define SET_FUNC(name, t, ...)    
    32 {
    33     if (ex_time(name##set.start_time, t)) {
    34         name##set.start_time = clock();
    35         name##set.func((void*)__VA_ARGS__);
    36     }
    37 }
    38 
    39 void func1(int a)
    40 {
    41     printf("func----1	[%d]
    ", a);
    42 }
    43 
    44 int func2(void)
    45 {
    46     printf("func----2
    ");
    47     
    48     return 0;
    49 }
    50 
    51 
    52 char* func3(int a)
    53 {
    54     while (a > 0)
    55         printf("func----3	[%d]
    ", a--);
    56     
    57     return "123";
    58 }
    59 
    60 
    61 int func4(int a, int b)
    62 {
    63     
    64     printf("func----4	[%d] %s [%d]
    ", a, ((a == b) ? "==" : ((a > b) ? ">" : "<")), b);
    65     
    66     return (a + b);
    67 }
    68 
    69 void func5(int a, char* str, int b, float c)
    70 {
    71     printf("func----5	[%d] "%s" [%d] [%f]
    ", a, str, b, c);
    72 }
    73 
    74 SET_INIT(func1);
    75 SET_INIT(func4);
    76 SET_INIT(func5);
    77 
    78 void main()
    79 {
    80     int func_addr = (int)func1;
    81     float value = 6.6;
    82     
    83     /*强制转换为函数指针类型*/
    84     ((void (*)(int))func_addr)(55);
    85     
    86     int start_time = clock();
    87     while (!ex_time(start_time, 2000)) {
    88         SET_FUNC(func1, 200, 10);
    89         SET_FUNC(func4, 500, 88, 99);
    90         SET_FUNC(func5, 500, 88, "hello", 99, value);
    91     }
    92 }
    View Code

    运行结果:

  • 相关阅读:
    JAVA消息对话框
    stringbuffer capacity()的疑问
    JAVA确认对话框
    c/c++实现获取NOD32升级账号密码
    复制构造函数(拷贝构造函数)
    使用VC将sqlite3.def转化为sqlite3.lib
    Windows下安装OpenSSL
    java中io与nio的使用
    使用 XStream 把 Java 对象序列化为 XML
    使用 XStream 把 Java 对象序列化为 XML
  • 原文地址:https://www.cnblogs.com/lgslearn/p/14237083.html
Copyright © 2011-2022 走看看