[C言語] 関数のポインタ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
 
int func_one() {
    printf("これは1つ目の関数です。\n");
    return 1;
}
 
int func_two() {
    printf("これは2つ目の関数です。\n");
    return 2;
}
 
int main() {
    int value;
    int (*function_ptr)();
 
    function_ptr = func_one;
    printf("function_ptr: 0x%08x\n", function_ptr);
    value = function_ptr();
    printf("戻り値: %d\n", value);
 
    function_ptr = func_two;
    printf("function_ptr: 0x%08x\n", function_ptr);
    value = function_ptr();
    printf("戻り値: %d\n", value);
 
    return 0;
}

$ ./a.out
function_ptr: 0xcdc907d4
これは1つ目の関数です。
戻り値: 1
function_ptr: 0xcdc907f4
これは2つ目の関数です。
戻り値: 2