Analysis of C language function pointer and callback function (a) "Go"

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/morixinguan/article/details/65494239

How to use and make static and dynamic libraries.

http://blog.csdn.NET/morixinguan/article/details/52451612

One of the concepts we're going to figure out today is called a callback function.

What is a callback function?

Baidu's authoritative explanation is as follows:

A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.

So we can look at an example:

[CPP]View PlainCopy  print?
    1. #include <stdio.h>
    2. void print ();
    3. int main (void)
    4. {
    5. void (*FUC) ();
    6. fuc = print;
    7. FUC ();
    8. }
    9. void print ()
    10. {
    11. printf ("Hello world!\n");
    12. }

As you can see from this example, we first define a function pointer fuc, the return value of this function pointer is void, and then we assign a value to the function pointer, which is print, which is the first address of the print function, when FUC obtains the address of print, The address of the fuc is equal to the address of print, so the final call to FUC () is equivalent to the call to print ();

Then I write this example obviously and Baidu explained that does not meet AH? The definition is if you pass the pointer (address) of the function as an argument to another function, and when the pointer is used to invoke the function it points to, we say this is a callback function, indeed, it is different, but the truth is the same, let's look at another example.

[CPP]View PlainCopyprint?
  1. #include <stdio.h>
  2. int Add_ret ();
  3. int Add (int A, int b, int (*add_value) )
  4. {
  5. return (*add_value) (A, b);
  6. }
  7. int main (void)
  8. {
  9. int sum = Add (3,4,add_ret);
  10. printf ("sum:%d\n", sum);
  11. return 0;
  12. }
  13. int Add_ret (int A, int b)
  14. {
  15. return a+b;
  16. }

From this example, we see:

Doesn't that fit our definition? We put the function pointer (address), here is Add_ret, as the parameter int add (int a, int b, int (*add_value)), here the argument is int (*add_value) (), the name can be arbitrarily taken, but to conform to the C language Naming conventions. When the pointer is used to invoke the function it points to, we say this is a callback function. We see the inside of the Add function, return (*add_value) (A, b); This (*add_value) (A, B) is equivalent to a simple reference to the pointer, we in the main function, we pass the function to implement the function, Add_ret, this function is simple, is to implement two numbers and return, here just good, simple reference, the equivalent to take out the value of the pointer return address, This value is the return a+b, which is the result of the addition of A and B two numbers that we pass in.

So what is the function of the callback function?

Speaking of this, there is a concept between the user and the developer, for example, just say the Add () This function, suppose that the user is to implement add_value this function, and the developer is to implement add_value this function, the user does not do much work, is to be implemented by the developer of such an interface , and then implement our functionality by invoking the return value of the interface implemented by the developer in the function. This developer role is a lot, can be the core of their own company development figures, can also be other work of the subcontractor's character, at this time, his role as a developer completely can be add_value implementation of Add_ret This function encapsulated and encrypted, Then throw a. So or. A to the user, then the user can not see the implementation of the specific Add_ret content, the user only need to provide him with a. h and. So, as a developer, he will be the function of his implementation of secrecy.

Next, we use Linux to demonstrate the result:

We created three files in the directory, Main.c,vendor.c,vendor.h

MAIN.C is a user-developed

VENDOR.C and Vendor.h are implemented by developers.

In Main.c, the code is as follows:

[CPP]View PlainCopyprint?
  1. #include <stdio.h>
  2. #include "Vendor.h"
  3. int Add (int A, int b, int (*add_value) )
  4. {
  5. return (*add_value) (A, b);
  6. }
  7. int main (void)
  8. {
  9. int sum = Add (3,4,add_ret);
  10. printf ("sum:%d\n", sum);
  11. return 0;
  12. }

VENDOR.C, the code is as follows:

[CPP]View PlainCopyprint?
    1. #include "Vendor.h"
    2. int Add_ret (int A, int b)
    3. {
    4. return a+b;
    5. }

Vendor.h, the code is as follows:

[CPP]View PlainCopyprint?
    1. #ifndef __vendor_h
    2. #define __vendor_h
    3. int Add_ret (int A, int b);
    4. #endif

Next, we make a dynamic link library, the final developer of the vendor.c content, the Vendor.h to provide users with.

Create dynamic-link libraries under Linux and package vendor.c and vendor.h into a dynamic-link library

First understand what the following commands mean:

To generate a dynamic library:

Gcc-shared-fpic Dvendor.c-o libvendor.so

-shared: Generate dynamic library;

-fpic: Generate the location-independent code;

-O: Specifies the generated target file;

Using Dynamic libraries:

GCC main.c-l. –lvendor-o Main

-L: Specifies the path of the library (at compile time); Use default path if not specified (/usr/lib/lib)

-lvendor: Specifies who the library needs to be dynamically linked to;

The code needs to load the dynamic library when it runs:

./main Load Dynamic library (default load path:/usr/lib/lib./...)

./main

After we compile the libvendor.so copy generated by the dynamic library to/usr/lib, we do not need to vendor.c now, we will remove the VENDOR.C, we can compile and execute the result of the main function, which is one of the functions of the callback function.

Analysis of C language function pointer and callback function (a) "Go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.