I also want to learn C language-Chapter 2: Return pointer values of the function and pointer to the function and logical and algorithm Separation

Source: Internet
Author: User

I have not studied today! I went to the hospital to complete my teeth. There is a hole in the teeth. However, I am still listening to the compilation language video tutorials of Sun Yat-sen University while filling my teeth. Although I can only listen to them, I still have some gains. Yesterday's post is very sorry, because a friend called gem sent a question, I was learning other knowledge and did not carefully read it! At that time, I understood what he meant wrong. I am here to apologize to you. The line of code I learned must be problematic, but in some loose scenarios, it can be easily expressed!

It's just two weeks since I learned the C language. I think the C language is quite awesome. We strive to end C language learning in three weeks. Of course, this is only about exploring the syntax principles. That is to say, at least each line of code I write knows the form in which the code exists in the memory. What do I think! Only after understanding this can we gradually exercise the internal strength of the C language. However, when we know the syntax principle, we can quickly lock the problem, because we know its architecture, and we can debug it to find errors.

Function that returns the pointer Value
1. the return value is a pointer-type data function, which is defined in the following form:

Type name * function name (parameter table)

For example: int * func (int x, int y );

Description: The Return Value of func is a pointer to int type data.

Note: * Before the function name only indicates that the range value is pointer-type data.

* There are no brackets on either side of func.

I will not write the example and debugging of the function that returns the pointer value, because I think this is very simple. I will focus on the knowledge points that point to the function pointer.

Pointer to function
Why is there a pointer to the function! As a program, a function also occupies a storage area in the memory! It has a starting address, that is, the function entry address, which is like a world map. Every country is like a space on a world map, if you look at a fixed place in one direction, each country will have one place closest to you. The closest place is like the function entry address, that's the starting address! This address is called the pointer of its function.

What is the role of a pointer to a function? In this way, we can define a pointer variable to point to a function and then call a function through a pointer. Pass functions as parameters between functions.

What are the benefits of using pointers to functions? : I don't know yet. I may know if I want to continue learning!

Since a function has one entry, How many exits does a function have? Let's just look at return! How many return statements are available!

Pointer variable definition to a function: type name (* pointer variable name )();

A more in-depth understanding of the pointer to the function: Because the compiler translates the function into the machine code, the first byte address of the machine code is the first address of the function, that is, the pointer to the function.

Indirect call and direct call: using a function pointer to call a function is called indirect call. Using a function name to call a function is called direct call.

Example:

Int (* p )()

P is a pointer variable pointing to the function. The return value of this function is int type.

Code example:

After reading this code, did you find a problem? p and Max are not the same. Why ?! Of course, you only need to search for information !...... 10 minutes later! It turns out to be like this. Let's look at this:

Oh! We understand! In 401005, there is a jmp jump. The original function pointer is summarized in a hop table! But this is only done in debug mode!

Let's take a look at the Code:

# Include <stdio. h> int Max (int x, int y) {return x> y? X: y;} void main () {int a; int (* p) (int) = NULL; p = (int (*) (int) Max; // Where is int (*) (int! It is a data type that indicates a pointer to a single argument function. It forces the address of one double argument function to be the address of one single argument function. A = (int (*) (int, int) p) (2, 9); // we cannot call it because the parameter is incorrect, so switch back again !} Wow! The type conversion of function pointers is depressing! But I have to learn it! It will certainly happen in the future!

Note: Since these pointer variables are equal to the entry address of a function, they are meaningless for addition and subtraction. (Hey hey! This sentence is just like Miss Tan! Although I have never read his book! But what do I think! These conclusions are just my understanding! There may be new understandings in the future! Come on !!!)

Separation of logic and Algorithm

  

We can see that our algorithms and logic are now available! In this case, we want to change the algorithm and the parameters! We parameterized the algorithm!

This note is written here! Come on! Learning knowledge makes life better!

 

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.