Understanding and use of C-language function pointers (learning)

Source: Internet
Author: User

1. Definition of function pointers


As the name implies, a function pointer is a pointer to a function. It is a pointer to a function. See Example:

123 A) char* (*fun1)(char * p1,char * p2);B) char * *fun2(char * p1,char * p2);C) char * fun3(char * p1,char* p2);

  

See what the above three expressions mean?


C) This is easy, Fun3 is the function name, P1,P2 is a parameter, its type is char * type, and the return value of the function is char * type.
B) is also very simple, compared to the C expression, the only difference is that the function's return value type is char**, is a two-level pointer.
A) is fun1 a function name? Recall the case where the array pointer was previously explained. We say that array pointers are so defined that they may be clearer:

1 int(*)[10] p;

And look at a) expression is similar to here! You got it. Here fun1 is not a function name, but a pointer variable, which points to a function. This function has two parameters of the pointer type, and the return value of the function is also a pointer. Again, let's rewrite this expression:

1 char* (*)(char * p1,char* p2) fun1;

Does this look good? It's just a pity the compiler doesn't think so. ^_^.

2. Example of function pointer use


We've defined a function pointer above, but how do we use it? Let's look at the following example:

12345678910111213141516171819202122232425 #include <stdio.h>#include <string.h>char* fun(char * p1,char* p2){  inti = 0;  i = strcmp(p1,p2);  if(0== i)  {    returnp1;  }  else  {    returnp2;  }}intmain(){  char* (*pf)(char * p1,char* p2);  pf = &fun;  (*pf) ("aa","bb");  return0;}

When we use pointers, we need to use the key ("*") to take the value of the memory it points to, and the function pointer is used as well. The function that exists on this address is removed by using (*PF) and then called.

It is important to note that in visual c++6.0, when assigning a value to a function pointer, you can use &fun or directly with the functions name fun. This is because the function name is actually an address after it is compiled, so there is no essential difference between the two usages. This example is very simple and is no longer discussed in detail.

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


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

1234567891011 void  function () { printf ( "call function!\n" }<BR> int   main () { void  < Code class= "Java Plain" > (*p) (); * ( int *) &p= ( int return  0 }


What the hell is this? * (int*) &p= (int) Function; what does that mean?
Don't worry, look at this line of code first:

1 void(*p)();

This line of code defines a pointer variable p,p to a function, and the function's 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 represents a pointer to an address cast exponentially to data of type int.
(int) The function represents the casting of the entry address of the functions into the data of type int.
Analysis here, believe you already understand * (int*) &p= (int) function; Indicates that the entry address of the function is assigned to the pointer variable p.


Then (*p) (); means a call to a function.


Explain to here, I believe you already understand. In fact, there is no difference between a function pointer and a normal pointer, only the content of the pointer is different.
The advantage of using a function pointer is that multiple modules that implement the same functionality can be identified together, which makes it easier to maintain later, and the system structure is clearer. Or summed up as: easy to layered design, conducive to system abstraction, reduce coupling and to separate the interface and implementation.

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


Do you feel the above example is too simple, not exciting? OK, let's have some excitement, look at the following example:

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

This is an example of the classic book "C Traps and Pitfalls". Not crazy, huh? Let's analyze and analyze:

1234 第一步:void(*) (),可以明白这是一个函数指针类型。这个函数没有参数,没有返回值。第二步:(void(*) ())0,这是将0强制转换为函数指针类型,0是一个地址,也就是说一个函数存在首地址为0的一段区域内。第三步:(*(void(*) ())0),这是取0地址开始的一段内存里面的内容,其内容就是保存在首地址为0的一段区域内的函数。第四步:(*(void(*) ())0)(),这是函数调用。


Seems to be very simple, right, the above example rewrite rewrite:

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


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

5. Array of function pointers

Now we know the expression

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

A function pointer pf is defined. Since PF is a pointer, it can be stored in an array. To modify the above style:

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

This is the definition of a function pointer array.

It is an array with an array named PF and 3 pointers to functions are stored in the array. These pointers refer to functions that return a value type to a pointer to a character, and a pointer to a character.

It seems a bit awkward to read. But it doesn't matter, the point is you understand that this is an array of pointers and arrays. How does an array of function pointers work? 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:

1234567891011121314151617181920212223242526272829 #include <stdio.h>#include <string.h><br>char* fun1(char* p){  printf("%s\n",p);  returnp;}char* fun2(char* p){  printf("%s\n",p);  returnp;}char* fun3(char * p){  printf("%s\n",p);  returnp;}<br>intmain(){  char* (*pf[3])(char* p);  pf[0] = fun1; //可以直接用函数名  pf[1] = &fun2; //可以用函数名加上取地址符  pf[2] = &fun3;<br>  pf[0]("fun1");  pf[0]("fun2");  pf[0]("fun3");  return0;} 

6. Pointers to array of function pointers


Looking at the headline, aren't you crazy? The function pointer is enough for beginners to toss, the function pointer array is 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 more detail here, and pointers to function pointers are not just pointers. But this pointer points to an array in which all the pointers to functions are stored. That's all.


The following defines a simple pointer to an array of function pointers:

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


Note that the PF and the PF in the previous section here are completely different. The PF in the previous section is not a pointer, but an array name; PF is really a real pointer here. This pointer points to an array containing 3 elements, which is a pointer to a function, which points to a function that returns a pointer to a character, and a pointer to a character.

This is more awkward than the array of function pointers in the previous section. In fact, you do not have to care so much, understand that this is a pointer to OK. Its usage is no different from the previous array pointers. A simple example is listed below:

123456789101112131415161718192021222324252627282930313233343536 #include <stdio.h>#include <string.h>char * fun1(char* p){    printf("%s\n",p);    returnp;}char* fun2(char* p){    printf("%s\n",p);    returnp;}char* fun3(char* p){    printf("%s\n",p);    returnp;}intmain(){    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");    return0;}

  

Reference: Chengzhong Teacher's "C Language Depth analysis".

Understanding and use of C-language function pointers (learning)

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.