int * p () int (* p) ();

** pointer to a function
int (* p) (); can be understood as the function is invoked (* p) This can be seen as a whole function name, the entry address of the representative function of p, c language () represents the function , so the p bracketed together changed into ( p) represents a function, for example:

#include <stdio.h>
int main()
{
    int num;
    int choice;
    int res;
    int *p = NULL;

    int change(int a,int (*p)(int a));
    int two(int a);
    int eight(int a);
    //int sixteen(int a); 

    printf("Enter an integer:");
    scanf("%d",&num);
    printf("Bin output press 1\
            Octal output press 2\
            Hex output press 3\
            Please enter your choice:\n");
    scanf("%d",&choice);

    if(choice == 1)
    {
        printf("Bin:");
        change(num, two);
    }
    if(choice == 2)
    {
        printf("Octal:");
        change(num, eight);
    }
    if(choice == 3)
    {
        printf("Hex:");
        printf("%X",num);
        //change(num, sixteen);
    }

    printf("\n");
    return 0;
}

int change(int a,int (*p)(int a))
{
    int res;
    res = (*p)(a);
    printf("%d\n",res);
}

int two(int a)
{
    int i,res=0,k=1;
    while( a )
    {
        i = a % 2;
        res = k*i + res;
        k = k*10;
        a = a /2;
    }
    return res;
}

int eight(int a)
{
    int i,res=0,k=1;
    while( a )
    {
        i = a % 8;
        res = k*i + res;
        k = k*10;
        a =  a / 8;
    }
    return res;
}

    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75

Examples of this practice can call a function pointer to a function and
this program function is to enter a number, then select (1 for binary output, 2 for octal, hexadecimal output 3) its output format.
Wherein the change function is a selection function, pressing the 1, 2 or 3 corresponding to the selected work function, the main function of the num value is passed to the first change, then that change then passed two other conversion functions, change in function, p is selected pointers to functions, main functions and other two first transfer function entry address in the parameter to change, change in its function is called.
We did not convert decimal to hexadecimal algorithms written
above the main program and then to change the course of two and
a simple program to deepen understanding a pointer to a function of

#include <stdio.h>
int main()
{
    int a = 1;
    int b = 1;
    int res;
    int add(int x,int y);
    int (*p)(int m, int n);

    p = add;
    res = (*p)(a, b);

    printf("res = %d\n", res);

    return 0;
}

int add(int x,int y)
{
    return (x + y);
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

** function returns a pointer value
next come to talk about the int p (), for ease of understanding, this can be replaced by p name of a function, such as fun, it
int the fun () calls this function is represented, this the return value is a pointer to function

#include <stdio.h>
int main()
{
    int week, day;
    int *GetDay(int *p_week, int  *p_day);
    int *p_week = NULL;
    int *p_day  = NULL;
    int *p;

    printf("Enter week day:\t");
    scanf("%d  %d", &week, &day);

    p_week = &week;
    p_day  = &day;
 
    p = GetDay(p_week, p_day);
    printf("\nIt is the %dest day of this month",*p);

    printf("\n");
    return 0;
}

int *GetDay(int *p_week, int *p_day)
{
    int week;
    int day;
    int temp, res;
    int *pt = NULL;
    week = *p_week;
    day  = *p_day;

    switch(week)
    {
        case 1:
            temp = 0;
            break;
        case 2:
            temp = 7;
            break;
        case 3:
            temp = 14;
            break;
        case 4:
            temp = 21;
            break;
        case 5:
            temp = 28;
            break;
    }

    res = day + temp;
    pt = &res;

    return (pt);
}

    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

** roughly means: you enter the first few days of the weeks of this month, the program will automatically convert it for the first few days of the month. Wherein the GetDay * int (int p_week, int p_day); return value is a pointer to a function. He will address the results of res return to the main function.
In the function call, only the address, in order to do two-way transfer of data, there is a bug, this should not be declared in the variable res GetDay function, this will cause the system to automatically recover its memory cells causes an error at the end of the function call.
The solution is simple, res one is defined as a global variable in the main header file; another way is to define the main function, declared in a function with extern GetDay.

Back to the topic, the paper base type is int, in fact, may be something else, like float, what char, if it is float, then returns a pointer to the data type is a float, the other the same way.
Original: https://blog.csdn.net/weixin_44421707/article/details/90722989

Guess you like

Origin blog.csdn.net/CSDN447447LJH/article/details/92241355