The use of C function pointers

Source: Internet
Author: User

1. The simplest usage:

1#include <cstdio>2 3 int(*p) (int);//define a function pointer variable p (the following f is actually a constant function pointer)4 intFintx)5 {6printf"%d\n", x+2);7     return 0;8 }9 Ten intMain () One { Ap=F; -P2);//equivalent to F (2) -     return 0; the}
View Code

There is another way of writing, but I really do not like it, can be ignored. Simply put, F () and (*f) () Implement the same function, and P pointers are no exception. I do not quite understand why C is allowed to write like this, simply do not remember this writing.

2. The second use:

1#include <cstdio>2 3typedefint(*p) (int);//Defining a function pointer type4 intFintx)5 {6printf"%d\n", x+2);7     return 0;8 }9 Ten intMain () One { AP pp;//defining function pointer variables -pp=F; -pp2);//equivalent to F (2) the     return 0; -}
View Code

The function pointer type is similar to the normal data type, I'm afraid you will think of it as a function parameter, well, try it very simple.

3. Address Jump:

void (*reset) (void) = (void (*) (void)) 0.

void (*reset) (void) is the function pointer definition, (void (*) (void)) 0 is a coercion type conversion operation, and the value "0" is cast to the function pointer address "0".

By calling the Reset () function, the program jumps to the "0" address at the execution of the program. In some other advanced microcontroller bootloader, such as Nboot, UBoot, eboot, often through the bootloader to download the program, and then through the function pointer to the address to execute the program.

1) void (*theuboot) (void);

。。。。
Theuboot = (void (*) (void)) (0x30700000);
Theuboot ();
。。。。。

2) (* (Void (*) (void))) (0x30700000)) ();

Force type conversions, convert an absolute address to a function pointer, and call this function to jump to the absolute address mentioned earlier.
Translation into a compendium is:
MOV r0,0x30700000;
MOV pc,r0

for ( * (void (*) (void))) (0x30700000));
You can understand that.

First (void (*) (void)) is a forced-type conversion, which forces the unsigned integer of the following 0x30700000 into a function pointer, which points to a function entry parameter of void and the return value is void. If you understand this step, then set (void (*) (void)) (0x30700000) is FP, then the above expression can be simplified to FP ().

Reference: http://blog.chinaunix.net/uid-25572546-id-2939029.html

The use of C function pointers

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.