Function pointer,

Source: Internet
Author: User
Tags define function

Function pointer,

1. First, let's talk about functions.

In fact, each function name is the function entry address, as shown in:

 

0x4013B0 is the entry address of the func () function. As you can see, func AND & func have the same address.

2. Use the function pointer to point to the above func () function.

Instance 1 is as follows:

#include "stdio.h"

int func (int x)
{
    return x;
}

int main ()
{
   int (* FP) (int); // Define FP function pointer

   FP = func; // FP points to the address of func

   printf ("% d \ n", FP (1));

   printf ("% p,% p \ n", FP, func);
 
   return 0;
}

Output result:

1 // call func () function
004013B0,004013B0

2) When typedef is used, the function pointer uses the following:

#include "stdio.h"

int func (int x)
{
    return x;
}

typedef int (* FP) (int); // Use declared function pointer type FP

int main ()
{
   FP a; // Define function pointer a via FP
  
   a = func; // a points to the address of func

   printf ("% d \ n", a (1));

   printf ("% p,% p \ n", a, func);

   return 0;
} 

 


Related Article

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.