Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master C language programming function pointer

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to master C language programming function pointer". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how to master C language programming function pointer"!

Catalogue

I. pointer primer

Second, use steps

1. Take the function address

two。 Create a function pointer

3. Two methods of calling a function through a function pointer

Third, the function pointer is advanced

I. pointer primer

Example: most of the pointers we often come into contact with are as follows:

Shaping pointer-stores the shaping address and points to the shaping

Character pointer-stores the character address, pointing to the character

Array pointer-holds the array address (note that it is not the first element address of the array), pointing to the array

From the above three examples, we can summarize what pointers have in common: the address of a variable of a certain type, pointing to a variable of that type, but first of all, there is a question about function pointers: does a function also have an address? Let's verify it with a simple piece of code.

# includeint Add (int xreint y) {return xreint;} int main () {printf ("% p\ n", & Add); return 0;}

The address is printed on the screen:

So the answer is yes, and the function also has an address, which leads to today's knowledge point-function pointer.

Second, use step 1. Take the function address

We know & the name of the array and the address of the array is taken out. A single array name takes out the address of the first element of the array. But for functions: function name = = & function name

Let's verify the code (example):

# includeint Add (int x pencil int y) {return x int y;} int main () {printf ("% p\ n", & Add); printf ("% p\ n", Add); return 0;}

Obviously, the address printed is the same, but at this time, some students will jump out and say, "the address printed by the array name and the & array name is still the same, but the meaning is obviously different." But if you think about it, the function does not have the first element and other things, ah, it is itself, it will not have any first element of the function.

So to state once again:

In the part of the function pointer, the function name = = & function name, its meaning and value are the same.

two。 Create a function pointer

We know that the array pointer is used to store the array address, and the shaping pointer is used to store the shaping address. Function pointer is no exception, it is used to store the function address, we now define a p to store the Add address, then how to create its type? Let's take a look at the specific steps:

1.p is a reference bar, give it a * is not necessary p becomes * p. In order to ensure the combination of * and p (if there are no parentheses, * or p may combine with some other symbols, see symbol priority), then it's okay for me to add a parenthesis outside * p for viewing, that is, (* p)

two。 Then the function has to have parameters. For example, here is Add (int x drawing int y). Parameters x and y are of type int

Do you want to find its parameters for the function you point to? So (* p) (int,int)

3. There is another property of the function, ah, is there a return value, and if so, what about the type? Here, take Add as an example. It returns int, so our pointer also returns int, that is, int (* p) (int,int).

At this point, the type of the Add function pointer is created as * int (p) (int, int).

It should be noted that the parameter types and return value types of different functions are different, so it is necessary to convert the types according to different functions. Here, we only take the Add function as an example, other functions, and so on.

Ps: a quick way to identify types-remove the name of the variable, and the rest is the type

The code is as follows (example):

Int a = 10 pf / remove a type int int arr [10] = {0}; / remove arr type int [10] int (* parr) [10] = & arr;// remove parr type int (*) [10], array pointer, array int (* pf) (int,int) = & Add;// remove pf type int (*) (int,int) 3. Two methods of calling a function through a function pointer

Law one:

When we usually call a function, it is usually the function name (,) and then pass the parameter into parentheses, so now we have a function pointer, how to use the pointer? P does not point to the function Add. We use * dereferencing pointer to get what is in the address, that is to say, * p==Add, passing parameters with * p (,) can also achieve the call to the Add function. The code is as follows:

# includeint Add (int x, int y) {return x + y;} int main () {int ret= Add (2,3); printf ("% d\ n", ret); / / ret=5 int (* p) (int, int) = & Add;//p is a pointer ret= (* p) (3,3) to the function Add / / ret=6 / / p points to Add, dereference to p is Add / / in short: * p=Add / / We don't always get the variable, sometimes we get the address of the variable / / the corresponding function pointer, sometimes we don't give you the function directly, give you the function address, just call printf ("% d\ n", ret);}

Law II:

We introduced in the part where we take the address of the function in 2. 1, in the part of the function pointer, the function name = = & function name, that is to say, when you create the function pointer, you can write: int (* p) (int, int) = Add,Add is assigned to p, you can also think: P is Add. You can understand it this way: method one is int (* p) (int, int) = & Add, which gives the address of Add to p, so calling a function with p requires dereference, but method two p is Add, so you don't have to dereference, just call it. The code is as follows:

# includeint Add (int x, int y) {return x + y;} int main () {/ / We know from the previous knowledge: when the function add takes the address, add=&add int (* p) (int, int) = Add / / assign Add to p, where p can be regarded as Add / / different from law one, which assigns & Add to p is the address of Add, so to dereference, p can be regarded as Add itself, and int ret = p (3,6); printf ("% d", ret) } / / if it is for ease of understanding, the first method is generally used, and if it is for convenience of operation, the second method can be used. Third, the function pointer is advanced.

Let's take a look at such a code (* (void (*) ()) 0) (). At first glance, it looks very complicated. Let's refine it.

1. (* (void (*) ()) 0) () We extract the bold part

This is an old friend we are familiar with: void (*) (). This is just a function pointer, which has no arguments and returns the type void.

2. What is (void (*) ()) 0? Let's think of (int) 3.14, which is to cast the type of 3.14, forcing the floating-point type of 3.14 to be shaped. The same thing here is to cast shaping 0 into a function pointer of type void (*) ().

3. Now that we have (void (*) ()) 0, we add a * in front of this thing. What does this mean? we know that (void (*) ()) 0 has been converted into a pointer (pointer is the address). The address is preceded by a * to denote dereference. Take out the contents of the address, that is, find the function.

4. (void (*) ()) 0 indicates the function, then adding a () after that is the call to the function, that is, (* (void (*) ()) 0) ()

Thank you for your reading, the above is the content of "how to master C language programming function pointer". After the study of this article, I believe you have a deeper understanding of how to master C language programming function pointer. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report