A brief description of the difference between pointer function and function pointer in C language _c language

Source: Internet
Author: User

The pointer function generally refers to the function that returns the pointer;

#include <stdio.h> 
 
int* fun (int *a) 
{return 
  A; 
} 
 
int main (int argc, char **argv) 
{ 
  int a = 3; 
  printf ("%d", * (Fun (&a)); 
  return 0; 
} 


A function pointer is a pointer to the start address of a function:
The first step is to understand the call procedure for a function:

#include <stdio.h> 
 
int fun (int i) 
{return 
  i + 1; 
} 
 
int main (int argc, char **argv) 
{ 
  int r; 
  r = Fun (5); 
  R = (*fun) (5);   Call Way 
  printf ("%d\n", r); 
  return 0; 
} 

A function can be invoked with R = (*fun) (5), which means that the function name is actually a pointer,
Addressed by (*fun). So we can define a pointer

#include <stdio.h> 
 
int fun (int i) 
{return 
  i + 1; 
} 
 
int main (int argc, char **argv) 
{ 
  int r; 
  Int (*FUNP) (int);  Declaration pointer 
  //FUNP = fun;    Assign values to pointers 
  FUNP = &fun; 
  r = FUNP (5); 
  printf ("%d\n", r); 
  return 0; 
} 


Therefore, there are two ways of assigning value to function pointers.
Similarly, there are two ways to call a function through a function pointer:

#include <stdio.h> 
 
int fun (int i) 
{return 
  i + 1; 
} 
 
int main (int argc, char **argv) 
{ 
  int r; 
  Int (*FUNP) (int);  Declaration pointer 
  FUNP = fun;   Assigning value to the pointer 
  //r = FUNP (5); 
  R = (*FUNP) (5);   Invoke 
  printf ("%d\n", r); 
  return 0; 
} 

That is, the fun () and (*fun) () are the same except where the declaration is made.
In this way, the C language is also easy to implement a structure similar to a callback function:

#include <stdio.h> 
 
int funa (int i) 
{return 
  i + 1; 
} 
 
int Funb (int i) 
{return 
  i-1; 
} 
 
void Fun (int (*FUNP) (int), int i) 
{ 
  printf ("%d\n", FUNP (i)); 
} 
 
int main (int argc, char **argv) 
{ 
  int (*FUNP) (int);  Declaration pointer 
  FUNP = Funa;    Assigning value to the pointer 
  //FUNP = FUNB;   Assign value to 
 
  the pointer Fun (FUNP, 5);    Call return 
  0; 
} 

  in the fun () function, it does just at some point call a FUNP pointer to the function, which function, in the definition of the fun function is not known, until the Funa assigned to function pointers FUNP,FUNP specifically what function to do to determine.
that is, the main function determines what function code the FUN function needs to help it implement, but fun when and whether to call the function that main gave him, which is determined by fun ().

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.