[C]Function and pointer analysis

function type

Functions in C language have their own specific types
The type of the function is determined by the return value, the parameter type and the number of parameters.

int add(int i,int j) is of type int(int,int)

Renaming function types through typedef in C language

typedef type name(parameter list)
example:
typedef int f(int,int);
typedef void p(int);

function pointer

A function pointer is used to point to a function
The function name is the entry address of the execution function body
Function pointers can be defined by function types: FuncType* pointer;
It can also be directly defined as: type (*pointer)(parameter list)

pointer is the function pointer variable name
type is the return value type of the referred function
parameter list is a list of parameter types of the referred function

interview questions

How to use C language to jump directly to a fixed address to start execution

via function pointer
Example 36-1

#include "stdio.h"

typedef int(FUNC)(int);
//typedef renames a new type
//FUNC stands for function A function type, this function type has an int parameter, and the return value is int
int test(int i)
{
    return i * i;
}
void f()
{
    printf("Call f()...\n");
}
int main()
{
    FUNC* pt = test;
    void(*pf)() = &f;    //pf is a pointer identifier, the function has no parameters, and the return value is null
                        //pf points to the entry address of function f
    printf("pf = %p\n",pf);
    printf("f = %p\n",f);
    printf("&f = %p\n",&f);
    pf();//Because pf is a function pointer, the function pointer saves the entry address of the function, and the function can be called directly
    (*pf)();//Because pf is a function pointer, the function pointer holds the entry address of the function equivalent to f();
    printf("Function pointer call:%d\n",pt(2));
    return 0;
}

Output result:
pf = 003B1005
f = 003B1005
&f = 003B1005
Call f()...
Call f()...
Function pointer call:4

Callback

A callback function is a calling mechanism implemented using function pointers
The principle of callback mechanism

The caller does not know the specific function that needs to be called when a specific event occurs
The called function does not know when to be called, only the task that needs to be done
When a specific event occurs, the caller calls the specific function through the function pointer

The caller and the called function in the callback mechanism are independent of each other

Example 36-2:

#include "stdio.h"
typedef int(*Weapon)(int);
void fight(Weapon wp, int arg)
{
    int result = 0;
    printf("Fight boss!\n");
    result = wp(arg);
    printf("Fight loss:%d\n",result);
}
int knife(int n)
{
    int ret = 0;
    int i = 0;
    for(i = 0;i < n; i ++)
    {
        printf("Knife attack:%d\n",1);
        ret ++;
    }
    return ret;
}
int sword(int n)
{
    int ret = 0;
    int i = 0;
    for(i = 0;i < n; i ++)
    {
        printf("sword attack:%d\n",1);
        ret += 5;
    }
    return ret;
}
int gun(int n)
{
    int ret = 0;
    int i = 0;
    for(i = 0;i < n; i ++)
    {
        printf("gun attack:%d\n",1);
        ret += 10;
    }
    return ret;
}
int main()
{
    fight(knife, 3);
    fight(sword,4);
    fight(gun,5);
}

Output result:
Fight boss!
Knife attack:1
Knife attack:1
Knife attack:1
Fight loss:3
Fight boss!
sword attack:1
sword attack:1
sword attack:1
sword attack:1
Fight loss:20
Fight boss!
gun attack:1
gun attack:1
gun attack:1
gun attack:1
gun attack:1
Fight loss:50

summary:

Functions in C language have specific types
Function pointers can be defined using function types
Function pointer is the key technology to realize callback mechanism
Fixed address jumps can be implemented in C programs through function pointers

Tags: C

Posted by daria on Mon, 30 May 2022 08:51:16 +0530