Questions about pointer functions and function pointers (1)

Source: Internet
Author: User

Function pointers and pointer functions are two different concepts, but they are always confusing. I hope all of the following will help you.

 

From a linguistic point of view, function pointers and pointer functions are both positive phrases. Therefore, the meanings of these two concepts are better understood.

 

Let's look at the pointer function first.

The so-called pointer function is essentially a function, but its return value is not a different value, but a pointer value (or an address ). Here we will first look at a function:

 

# Include <stdio. h>
Int * Hello (int A, int B ){
Int c = A + B;
Return & C;
}
Int main (){
Int A = 5;
Int B = 6;
Int * ipointer;
Ipointer = Hello (A, B );
Printf ("% 0x/N", ipointer );
Printf ("% d/N", & ipointer );
Return 0;
}

 

Let me imagine what the results of the two output statements of this function will look like? Of course, we only know the first output statement because it is an address value, but we do not know the specific address value. For the second output statement, what we want is to get the value of A + B (that is, 11 ). Think about it. Can we really get the desired value? Maybe. Maybe not. Let's take a look at the output results:

 

 

This is the two output results. Why is the second output not 11? This problem will be discussed for the moment. Here we will first look at another problem. The above program has the following function:

 

Int * Hello (int A, int B ){
Int c = A + B;
Return & C;
}
Can we directly rewrite the return & C; into Return & (a + B? Of course, anyone who has a little programming experience knows that this is of course not feasible. But here I want to talk about it, it should be because we are usually used to returning some common variables, so often there will be return A + B; in this way, we are prone to initiate a habitual thinking and return a + B; (of course, the reason is, A + B is returned as the right value as a constant, and the return value required by this function is a pointer and an address value. Therefore, we must return a left value instead of the right value ).

 

Here we should solve the problem that the second output we encountered was not the expected result. Think carefully. Before resolving this issue, let's make the following changes to the above Code:

 

# Include <stdio. h>
Int C;
Int * Hello (int A, int B ){
C = A + B;
Return & C;
}
Int main (){
Int A = 5;
Int B = 6;
Int * ipointer;
Ipointer = Hello (A, B );
Printf ("----- % 0x -----/N", ipointer );
Printf ("----- % d -----/N", * ipointer );
Return 0;
}

 

Here let's take a look at the results:

 

Here we get the expected 11, which can tell us the problem compared with the above Code. Of course, this involves the scope. Of course, the scope is simple, but when the scope is combined with other concepts, it becomes more complicated. I hope I will have a chance in the future, to discuss the scope issues.

 

Next, let's continue with another part of our topic, the function pointer. Needless to say, we already know through the literal meaning that a function pointer is actually a pointer to a function. Its essence is a pointer, But it points to a function. In this case, how can we define how to use function pointers? Most people have to explore this issue by themselves. It should be that pointers do not exist in many programming languages, the basic course C/C ++ is rarely involved. Next, let's take a look at the definition of function pointers:

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

 

The first is the data type identifier. It refers to the type of the return value that the function pointer points to, and then (the pointer variable surface). Why is there a bracket? This is related to the combination of symbols and the priority of operators. We know that the priority of parentheses is higher than that of pointers. Therefore, if there are no parentheses, the priority pre-algorithm will become a pointer function. Of course, the form parameter list is variable, and of course there is no form parameter, but when we use it, note that the parameters of the function pointed to by the function pointer must have the same correspondence with the list of parameters of the function pointer. Otherwise, compilation fails. For specific usage, look at the following small program, it will not be too long.

 

# Include <stdio. h>
INT (* test) (int A, int B );
Int Hello (int A, int B ){
Return A + B;
}

Int main (){
Int A, B;
A = 5;
B = 6;
Test = Hello;
Printf ("= % 0x =/N", & test );
Printf ("= % 0x =/N", test );
Printf ("= % 0x =/N", * test );
Int c = test (A, B );
Return 0;
}

 

 

 

The calculation result is as follows:

 

Let's take a closer look at how the results of test and * test are the same? This issue will be discussed again next time.

 

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.