Deep analysis of the definition of function pointer in C language and the use of _c language

Source: Internet
Author: User

1. Definition of function pointer
a function is a sequence of instructions or code that consists of executing statements. The ordered collection of these codes is allocated to a certain memory space according to its size, the starting address of this memory space becomes the address of the function, different functions have different function addresses, and the compiler uses the function name to index the entry address of the function. In order to facilitate the operation of the same function of the type attribute, C + + introduces the function pointer, which is a pointer to the code entry address and a pointer variable to the function. Thus the "function pointer" itself should first be a pointer variable, except that the pointer variable points to a function. This is just like using pointer variables to point to shaping variables, character types, arrays, and here is the point function. C at compile time, each function has an entry address, which is the address that the function pointer points to. When you have a pointer variable that points to a function, you can call a function with that pointer variable, as if you were using a pointer variable to reference another type variable, which is consistent in these concepts. function pointers have two uses: calling a function and doing a function's arguments.

The method of declaring a function pointer is:

Data type identifier (pointer variable name) (parameter list);

Function type describes the return type of a function, because the precedence of "()" is higher than "*", so the parentheses outside the pointer variable name are necessary, and the following "parameter list" represents the list of parameters that the function that the pointer variable points to. For example:

int function (int x,int y); /* Declare a function/

int (*f) (int x,int y);/* Declare a function pointer//

f=function; * Assign the first address of the function functions to the pointer f */

When assigned, function functions do not have parentheses or parameters, because function represents the first address of the functions, so after the assignment, the pointer f points to function functions (int x,int y), the first address of the code.

2. Example of using function pointers
know how to define a function pointer, but how do you use it? Let's look at the following examples:

#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 pass the key ("*") to the value in the memory it points to, and so does the function pointer. Use (*PF) to remove a function that exists on this address, and then call it.

It should be noted here that in Visual c++6.0, when assigning a function pointer, you can use &fun or directly with the fun of the functions name. This is because the function name is actually an address after being compiled, so there is no essential difference between the two usages. This example is very simple and will not be discussed 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 the hell is going on here? * (int*) &p= (int) Function; what does that mean?
Don't worry, look at this line of code first:

void (*p) ();

This line of code defines a pointer variable p,p point to a function where both the parameter and the return value are void.
&p is to find the address of the pointer variable p itself, which is a 32-bit binary constant (32-bit system).
(int*) &p represents a pointer to cast an address to a exponentially to an int type of data.
(int) A function represents a force that converts the entry address of the functions to data of type int.
Analysis here, I believe you have understood * (int*) &p= (int) function; Represents assigning the entry address of a function to the pointer variable p.


So (*p) (); Is a call to a function.


Explain to here, I believe you already understand. In fact, the function pointer is no different from the normal pointer, just the content of the point.
The advantage of using function pointers is that multiple modules that implement the same functionality can be identified together, which makes it easier to maintain later and to make the system more clearly structured. Alternatively, it is convenient for layering design, for system abstraction, for reducing coupling degree and for separating interface from implementation.

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

Do you feel that the above example is too simple and not exciting enough? OK, let's have a little bit of stimulation, look at the following example:

(* (Void (*) ()) 0) ();

This is an example of the classic book "C Traps and pitfalls". Not crazy, huh? Here's what we'll analyze:

The first step: void (*) (), you can see that this is a function pointer type. This function has no parameters and no return value.
Step Two: (Void (*) ()) 0, which is casting 0 to function pointer type, 0 is an address, that is, a function exists within a section of the first address 0.
Step Three: (* (Void (*) ()) 0, which is the content of a section of memory that starts with a 0 address, which is a function stored in a section of the first address 0.
Fourth Step: (* (Void (*) ()) 0) (), this is a function call.

It seems to be quite simple, right, the example above rewrite rewrite:

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

If there is no above analysis, Ken is afraid it is not easy to see the expression of it. But it should be a simple matter now. What did the reader think?

5. Array of function pointers

Now we know the expressions

char * (*PF) (char * p);

The definition is a function pointer pf. Since PF is a pointer, it can be stored in an array. To modify the formula:

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

This is the definition of an array of function pointers.

It is an array with the array named PF, and the array stores 3 pointers to the function. These pointers point to a function that returns a pointer to a character with a value type, a pointer to a character, and a parameter.

It seems a bit clumsy to read. But it doesn't matter, the key is that you understand that this is an array of pointers. How do you use a function pointer array? Here is also a very simple example, as long as the real grasp of the use of methods, and then complex problems can be addressed.

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;  Can directly use
the function name pf[1] = &fun2;//Can be used to add the address character
pf[2] = &fun3;<br>
pf[0] ("fun1");
Pf[0] ("fun2");
Pf[0] ("Fun3");
return 0;
}

6. Pointer to array of function pointers


Look at this headline, aren't you crazy? function pointers are enough for beginners to toss, the function pointer array is even more troublesome, now the function pointer array pointer is more difficult to understand.
Actually, it's not so complicated. The question of several sets of pointers is discussed in detail, and the function pointer array pointer here is not a pointer. But this pointer points to an array that contains pointers to functions. That's all.


A simple array of function pointers is defined below:

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

Note that the PF and the PF in the previous section are completely different. The PF in the previous section is not a pointer, but an array name; The PF here is indeed a real pointer. This pointer points to an array that contains 3 elements, which contain pointers to functions that return a pointer to a character, a pointer to a character, and an argument.

This is more awkward than the array of function pointers in the previous section. In fact, you don't have to care so much, understand that this is a pointer on the OK. The usage is no different from the array pointers mentioned earlier. A simple example is listed below:

#include <stdio.h>
#include <string.h>
 
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;
}
 
int main ()
{
  char * (*a[3]) (char * p);
  char * (* (*PF) [3]) (char * p);
  PF = &a;
 
  A[0] = fun1;
  A[1] = &fun2;
  A[2] = &fun3;
 
  Pf[0][0] ("fun1");
  Pf[0][1] ("fun2");
  Pf[0][2] ("Fun3");
  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.