In depth analysis of the definition and use of function Pointers in C language

  • 2020-05-10 18:29:39
  • OfStack

1. Definition of function pointer    
  function is composed of statement execution sequences of instructions or code, the code of an ordered set according to their size are assigned to 1 set of memory space, this 1 piece of the starting address of the memory space is the address of the function, different functions have different address, the compiler entry address of index function by the function name, the same function for the convenience of operation type attributes, c/c + + introduced a function pointer, the function pointer is a pointer to the entry address code, is a pointer to the function variables. So the "function pointer" itself should first be a pointer variable, but the pointer variable points to the function. This is like using pointer variables to point to plastic variables, character types, array 1, in this case to point to the function. At compile time, C has an entry address for each function, which is the address to which the function pointer points. Once you have a pointer variable to a function, you can call the function using that pointer variable, just as a pointer variable can refer to a variable of another type 1, which in these concepts is 1. Function Pointers are used for two purposes: to call functions and to take arguments to functions.

The declaration method of function pointer is as follows:


 Datatype identifier  ( Pointer variable name ) ( The parameter list );

"Function type" indicates the return type of the function. Since "()" has a higher priority than "*", parentheses outside the pointer variable name are necessary. The "parameter list" after the pointer variable refers to the parameter list of the function. Such as:


  int function(int x . int y); /*  The statement 1 A function  */

  int (*f) (int x . int y); /*  The statement 1 Function pointer  */

  f=function; /*  will function The first address of the function is assigned to the pointer f */

When the function function is assigned, there are no parentheses and no parameters. Since function represents the first address of the function, the pointer f points to the function function(int x, int y) after the assignment. The first address of the code.

2. Examples of function Pointers
You know how to define a function pointer, but how to use it? Here's an example:


#include <stdio.h>
#include <string.h>
 
char * fun(char * p1,char * p2)
{
  int i = 0;
  i = strcmp(p1,p2);
 
  if (0 == i)
  {
    return p1;
  }
  else
  {
    return p2;
  }
}
 
int main()
{
  char * (*pf)(char * p1,char * p2);
  pf = &fun;
  (*pf) ("aa","bb");
  return 0;
}

When we use a pointer, we need to use the key (" * ") to get the value it points to in memory, and the same is true for function Pointers. By taking the function stored at this address and calling it with (*pf).

Note here that in Visual C++6.0, you can assign a pointer to a function using &fun or directly using the function name fun. This is because the function name is compiled to an address, so there is no essential difference between the two USES. This example is so simple that I won't go into it in detail.

3.*(int*)&p -- what is this?

Perhaps the above example is too simple, let's look at the following example:


void Function()
{
  printf("Call Function!\n");
}<br>
int main()
{
  void (*p)();
  *(int*)&p=(int)Function;
  (*p)();
  return 0;
} 

What is this doing? * (int *) & p = (int Function); What does it mean?
Take a look at this line of code:


void (*p)();

This line of code defines a pointer variable p, and p points to a function whose arguments and return values are void.
&p is the address of the pointer variable p itself, which is a 32-bit binary constant (32-bit system).
(int*)&p means to cast the address into a pointer to data of type int.
(int)Function means to cast the entry address of a function into data of type int.
At this point of the analysis, I believe you have understood that *(int*)&p=(int)Function; Represents assigning the entry address of a function to the pointer variable p.


So p (*) (); That's the call to the function.


So far, I think you get the idea. In fact, function Pointers and ordinary Pointers are no different, just pointing to different content.
The advantage of using function Pointers is that you can identify multiple modules with the same function as 1, so that 1 can be more easily maintained and the system structure is clearer. Or it can be summarized as: facilitate layered design, facilitate system abstraction, reduce coupling, and separate interface from implementation.

4.(*(void(*) ())0)()-- what is this?

Do you feel that the above example is too simple and not exciting enough? Well, for a bit of excitement, here's an example:


(*(void(*) ())0)();

This is an example from the classic book C Traps and Pitfalls. You're not crazy, are you? Here's our analysis:

Step 1: void(*) (), you can see that this is a function pointer type. This function takes no arguments and returns no value.
Step 2 :(void(*) ())0, this is to cast 0 into the function pointer type, 0 is an address, that is, 1 function is stored in the first address of 0 in the 1 section.
Step 3 :(*(void(*) ())0), which is the function stored in the first segment of memory starting from address 0.
Step 4 :(*(void(*) ())0)(), this is the function call.

So it's pretty simple, right? Let's rewrite the above example:


(*(char**(*) (char **,char **))0) ( char **,char **);

Without the above analysis, it would be difficult to understand this expression. But it should be an easy one now. What do the readers think?

5. Function pointer array

Now we know the expression


char * (*pf)(char * p);

It defines a function pointer pf. Since pf is a pointer, it can be stored in an array. Modify the above formula 1 below:


char * (*pf[3])(char * p);

This is the definition of 1 function pointer array.

It is an array called pf that stores three Pointers to functions. These Pointers point to functions that return a pointer to a character of type one and take a pointer to a character of parameter one.

It seems like a mouthful to read. But it doesn't matter, the point is you understand that this is an array of Pointers, it's an array. How do you use an array of function Pointers? Here is also given a very simple example, as long as you really master the use of the method, then complex problems can be dealt with.

As follows:


#include <stdio.h>
#include <string.h>
<br>char * fun1(char * p)
{
  printf("%s\n",p);
  return p;
}
 
char * fun2(char * p)
{
  printf("%s\n",p);
  return p;
}
char * fun3(char * p)
{
  printf("%s\n",p);
  return p;
}
<br>int main()
{
  char * (*pf[3])(char * p);
  pf[0] = fun1; // You can just use the function name 
  pf[1] = &fun2; // You can use the function name plus the fetch address character 
  pf[2] = &fun3;<br>
  pf[0]("fun1");
  pf[0]("fun2");
  pf[0]("fun3");
  return 0;
} 

 

6. Pointer to function pointer array


You're not crazy about this headline, are you? Function Pointers are enough for beginners to play around with, function pointer array is more troublesome, now the function pointer array pointer is more difficult to understand.
Actually, it's not that complicated. The pointer to an array, discussed in detail earlier, is a pointer to a function. Except that this pointer points to an array that contains Pointers to functions. That's all.


Here is a simple function pointer array pointer:


  int function(int x . int y); /*  The statement 1 A function  */

  int (*f) (int x . int y); /*  The statement 1 Function pointer  */

  f=function; /*  will function The first address of the function is assigned to the pointer f */

0

Notice that pf here is completely different from pf in the last video. pf in the previous section is not a pointer, but an array name; pf here is really a real pointer. This pointer points to an array containing three elements; This number is a pointer to a function; These Pointers point to functions that return a pointer to a character of type one and take a pointer to a character of parameter one.

This is even more of a mouthful than the function pointer array in the previous section. You don't really have to worry about that much, but if you know that this is one pointer, it's ok. Its use is no different from that of array Pointers. Here is a simple example:


  int function(int x . int y); /*  The statement 1 A function  */

  int (*f) (int x . int y); /*  The statement 1 Function pointer  */

  f=function; /*  will function The first address of the function is assigned to the pointer f */

1


Related articles: