In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the function pointer in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the function pointer in C language.
1. Definition of function pointer
As the name implies, a function pointer is a function pointer. It is a pointer to a function. Look at the example:
A) char * (* fun1) (char * p1Magna char * p2); B) char * * fun2 (char * p1Magna char * p2); C) char * fun3 (char * p1Magna char * p2)
What do the above three expressions mean?
C) this is easy, fun3 is the function name, p1 and p2 is the parameter, its type is char *, and the return value of the function is char *.
B) is also very simple, the only difference from C) expressions is that the return value type of the function is char**, which is a secondary pointer.
A) is fun1 a function name? Recall what happened when I explained array pointers earlier. We say that the definition of array pointers might be clearer:
Int (*) [10] p
And look at A) how similar the expression is here! You got it. Here fun1 is not a function name, but a pointer variable that points to a function. This function has two parameters of pointer type, and the return value of the function is also a pointer. Again, let's rewrite this expression:
Char * (*) (char * p1dchar * p2) fun1
Doesn't it look better like this? It's a pity that the compiler doesn't think so. ^ _ ^.
two。 Examples of the use of function pointers
Above we define a function pointer, but how to use it? Let's take a look at the following examples:
# include # include char * fun (char * p1magchar * p2) {int I = 0; I = strcmp (p1Magna p2); if (0 = = I) {return p1;} else {return p2;}} int main () {char * (* pf) (char * p1m char * p2); pf = & fun; (* pf) ("aa", "bb"); return 0;}
It should be noted here that in Visual pointer 6.0, when assigning a value to a function pointer, you can use & fun or directly use the function 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 uses. This example is very simple and will not be discussed in detail. When we use a pointer, we need a key ("*") to get the value in the memory it points to, and the same is true for the use of function pointers. Take out the function that exists at this address with (* pf) and call it.
If you think the text is not easy to absorb, you can watch it with the following video.
So that you are no longer afraid of pointers in C language (part 1) _ bilibili ("-") "cheers ~-bilibiliwww.bilibili.com"
3. * (int*) & p-what is this?
Perhaps the above example is too simple, let's take a look at the following example:
Void Function () {printf ("Call Function!\ n");} int main () {void (* p) (); * (int*) & p = (int) Function; (* p) (); return 0;}
What is this? What does * (int*) & p = (int) Function; mean?
Don't worry, take a look at this line of code:
Void (* p) ()
This line of code defines a pointer variable pforce p that 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 an address into a pointer to data of type int.
(int) Function means to force the entry address of a function to be converted to data of type int.
At this point of analysis, I believe you have understood that * (int*) & p = (int) Function; means to assign the entry address of the function to the pointer variable p.
Then (* p) (); represents the call to the function.
This is the end of the explanation, I believe you already understand. In fact, the function pointer is no different from the ordinary pointer, only the content of the point is different.
The advantage of using function pointers is that multiple modules that achieve the same function can be identified together, which makes it easier to maintain later, and the system structure is clearer. Or it can be summarized as: it is convenient for hierarchical design, conducive to system abstraction, reduce the degree of coupling and separate the interface from the implementation.
Recommend your own linuxC/C++ communication group: 973961276! Sorted out some personal feel better learning books, video materials and large factory noodle video sharing in the group file, the need for partners can add their own oh! ~
4. (* (void (*) ()) 0) ()-what is this?
Do you feel that the above example is too simple and not exciting enough? OK, let's do something exciting. Take a look at the following example:
(* (void (*) ()) 0) ()
This is an example from the classic book C Traps and Pitfalls. Not crazy, are you? Let's analyze and analyze:
The first step: void (*) (), you can see that this is a function pointer type. This function has no arguments and no return value. The second step: (void (*) ()) 0, which converts 0 to a function pointer type. 0 is an address, which means that a function exists in an area with a first address of 0. The third step: (* (void (*) ()) 0), which is the content of a section of memory starting with the address 0, and its content is the function stored in the area with the first address of 0. Step 4: (* (void (*) ()) 0) (), which is a function call.
It still seems very simple, right? the above example is rewritten again:
(* (char** (*) (char**, char**)) 0) (char**, char**)
Without the above analysis, I am afraid it will not be easy to understand this expression. But it should be a very simple thing now. What do readers think?
5. Function pointer array
Now we know the expression.
Char * (* pf) (char * p)
A function pointer pf is defined. Since pf is a pointer, it can be stored in an array. Modify the above formula:
Char * (* pf [3]) (char * p)
This is to define an array of function pointers.
It is an array called pf with three pointers to the function stored in the array. These pointers point to functions that return a pointer to a character and an argument to a pointer to a character.
It seems a bit of a mouthful to read. But it doesn't matter, the point is that you understand that this is an array of pointers, an array. How to use an array of function pointers? A very simple example is also given here. As long as you really master the method of use, even complex problems can be dealt with.
As follows:
# include # include 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 * (* pf [3]) (char * p); pf [0] = fun1; / / you can directly use the function name pf [1] = & fun2; / / you can add the address character pf [2] = & fun3; pf [0] ("fun1"); pf [0] ("fun2") Pf [0] ("fun3"); return 0;} 6. The pointer to an array of function pointers
Are you crazy looking at this title? Function pointers are enough for ordinary beginners, the function pointer array is more troublesome, and now the function pointer array pointers are more difficult to understand.
Actually, it's not that complicated. The problem of array pointers has been discussed in detail earlier. The function pointer array pointer here is just a pointer. It's just that this pointer points to an array that contains pointers to functions. That's all.
Let's define a simple function pointer array pointer:
one
Char * (* (* pf) [3]) (char * p)
Note that the pf here is completely different from the pf in the previous section. The pf in the previous section is not a pointer, but an array name; the pf here is really a pointer. This pointer points to an array of three elements; this number contains pointers to functions; these pointers point to functions that return a pointer to a character and an argument to a pointer to a character.
This is more mouthful than the function pointer array in the previous section. As a matter of fact, you don't have to worry so much, just ok if you understand that this is a pointer. Its usage is no different from the array pointer mentioned earlier. Here is a simple example:
# include # include 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 * (* pf [3]) (char * p); pf [0] = fun1; / / you can directly use the function name pf [1] = & fun2; / / you can use the function name plus the address character pf [2] = & fun3
Pf [0] ("fun1"); pf [0] ("fun2"); pf [0] ("fun3"); return 0; at this point, I believe you have a deeper understanding of "how to understand function pointers in C". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.