Difference function pointers and pointers to functions of language c

Function pointer: a pointer to a function variable, so the function pointer itself should be a pointer variable, but the pointer variable points to the function.

Target function: the function pointer with, i.e. is essentially a function.

Examples of function pointers:

#include<stdio.h>
#include<iostream>
int max(int, int);
int min(int, int);
int add(int, int);
int process(int a, int b, int(*func)(int a, int b));
int main() {
    int a, b;
    the printf ( " Please enter the values a, b, and separated by a space: \ n- " );
    scanf_s("%d %d", &a, &b);
    printf("a=%d,b=%d,max=%d\n", a, b, process(a,b,max));
    printf("a=%d,b=%d,min=%d\n", a, b, process(a, b, min));
    printf("a=%d,b=%d,add=%d\n", a, b, process(a, b, add));
    system("pause");
    return 0;
}
int max(int a, int b) {
    if (a >= b) {
        return a;
    }
    else
    {
        return b;
    }
}
int min(int a, int b) {
    if (a >= b) {
        return b;
    }
    else
    {
        return a;
    }
}
int add(int a, int b) {
    return a + b;
}
int process(int a, int b, int(*func)(int a,int b)) {
    return (*func)(a, b);
}

Example pointer to the function:

#include<stdio.h>
#include<iostream>
#include<string.h>

char * initMemory () {
     // open 32 bytes of memory, and with the character pointed to by s memory 
    char * s = ( char *) the malloc ( the sizeof ( char ) * 32 );
     return s;
}

int main () {
     // definition of a function returns a value received pointer 
    char * PTR = initMemory ();
    strcpy(ptr, "hello world");
    the printf ( " % S \ n- " , PTR);
     // release the memory 
    free (ptr);
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/xiximayou/p/12126393.html