Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C language function pointer and a callback function     - Using DBMS_STAT function closes mission (Database)

- ThinkPad X220 Ubuntu 14.10 installed on fingerprint recognition (Linux)

- Teach you how to choose to install CentOS 6.5 installation package (Linux)

- To teach you how to safely send mail (Linux)

- Use Git in Eclipse (Linux)

- Linux command execution order control and pipeline (Linux)

- Linux common network tools: traceroute routing of scanned (Linux)

- How to install and configure the KVM and Open vSwitch on Linux distributions (Linux)

- Change all child files and subdirectories in the owner permissions Ubuntu (Linux)

- Ubuntu GCC, G ++ and fortran Version Switch (Linux)

- ActiveMQ-based shared file system HA solutions (Server)

- Update GAMIT10.6 command (Linux)

- Cacti Linux-based system monitoring and alarm (Linux)

- Linux NIC configuration (Linux)

- VNC configuration detailed analysis under Linux (Linux)

- PostgreSQL query result area is removed and precision (Database)

- Linux supports serial output method (Linux)

- To build PHP environment (Nginx + MariaDB + PHP7) under CentOS 6.5 (Server)

- Ubuntu 14.04 Docker installation (Linux)

- CentOS 6.5 install Maven and Nexus warehouse agent (Server)

  C language function pointer and a callback function
  Add Date : 2017-08-31      
  In a hierarchical design program, the upper module can directly call the underlying function module, while the lower module can not be directly calling the function of the upper module. And in reality it is often the case interdependence exists between layers, i.e., the function is called inter-layer, e.g., changes in the state of the layer B needs to notify the layer A or layer B is caused by a state change, in order to avoid this interdependence can use the callback function. Suppose A is the upper layer of layer B, layer A function call level B, the layer A is called caller, layer B function is called is called callee, layer A is callee callback function is called callbacker.

1. callback function

The callback function is to achieve the transfer callbacker callee function pointers by caller, when the callee is called the callbacker, called a callback occurs, the callback function is called and callbacker. callee without concern callbacker implementation details and specific type of data being handled, the prototype can only know callbacker, and callbacker implemented by the caller is responsible for, including implementation details (algorithm) and data types.

The callback function can realize dynamic binding at runtime through the callee pass different function pointer, to invoke different functions. For example, sorting algorithms compare data required by certain rules, callee need to know the data comparison method and the type of data, but only care about the number and the meaning of the comparison result of the comparison data, the specific comparison operation by the callbacker responsible for, data types can be primitive data types can also be a structure type.

Callback message notification can be realized and event-driven, such as when an event occurs callee, the need to inform the caller or the caller needs to perform certain functions, it can be achieved through a callback mechanism.

2. The function pointer

Callback mechanism is realized by passing a function pointer, and the function pointer is a pointer to a function, define the function pointer can be used in two forms:

(1) defined directly

Function return type (* function pointer name) (parameter list);

(2) the use of typedef

typedef function return type (* new type name) (parameter list);

Type the name of the new function pointer name;

int add_int (int a, int b) {return a + b;}

int (* pfunc) (int x, int y);

pfun = add_int; // or & add_int

typedef int (* PFUNC) (int x, int y);

PFUNC pfunx;

pfunx = & add_int;

3. Return function pointers

That function's return value is a function pointer, it can be defined in two forms:

(1) defined directly

Function return type (* function name (parameter list 1)) (parameter Table 2) {......}

Defines a function name from "function" identified by the parameters "parameter in Table 1" logo, the function returns a function pointer to a by a "function return type" identifies the type of return, in line with the number of parameters and type " parameter list 2 "function.

(2) the use of typedef

typedef function return type (* new type name) (parameter Table 2);

Type the name of the new function name (parameter list 1) {......}


void (* signal (int sig, void (* func) (int))) (int);

Function name: signal

Function: The function specified signal processing, sig designation signal, func as a function of the signal processing with an integer parameter

Returns: a function pointer having an integer argument, the return value of a function of type void, consistent with the function parameter types and return type of the function func; that returns the previous signal handler

#include < signal.h>

char tmpfilename [L_tmpnam];

void terminate (int param)
  printf ( "Terminating program ... \ n");
  remove (tmpfilename); // remove temporary files
  exit (1);

int main (void)
 void (* prev_fn) (int); // define the function pointer
 FILE * fp;

 prev_fn = signal (SIGTERM, terminate); // SIGTERM signal to terminate the specified handler

 tmpnam (tmpfilename); // generate temporary file name and save the tmpfilename

 fp = fopen (tmpfilename, "w +");
 fprintf (fp, "test");
 fclose (fp);

 raise (SIGTERM); // generates SIGTERM signal and notification process

 if (prev_fn == SIG_IGN) signal (SIGTERM, SIG_IGN); // reset default handler

 return 0;

4. The array of function pointers

Pointing to a group of the same type, and the function returns the number of arguments and order, commonly used to replace switch / if the structure may be defined in two forms:

(1) defined directly

Function return type (* function pointer array name [N]) (parameter list);

(2) the use of typedef

typedef function return type (* new type name) (parameter list);

Type the name of the new function pointer array name [N];
- wget command Walkthrough (Linux)
- Replace font under Linux (Linux)
- Visual Studio Code experience (Linux)
- CentOS 6.7 install Nagios Tutorials (Server)
- CentOS card fails to start at boot progress bar certmonger solve (Linux)
- Five programming fallacy (Programming)
- Configuring Sublime Text Python runtime environment 2 (Linux)
- Various sorting algorithms implemented in Python (Programming)
- Linux operating system must know the security command (Linux)
- Use the TC flow control test under Linux (Linux)
- How to build Mono 3.4.0 / 3.4.1 on Windows (Linux)
- Performance comparison Fibonacci recursive and non-recursive (Programming)
- 256 with rich colors decorate your terminal (Linux)
- Install RAID 6 (Striping double distributed parity) (Linux)
- Ubuntu configuration SVN and http mode access (Server)
- Linux OOM killer mechanism (Linux)
- MySQL view (Database)
- Sysdig: system troubleshooting tool (Linux)
- Using Linux command line and execute PHP code (Programming)
- Write perfect printf (Programming)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.