[C Language] 14-return pointer function and pointer to function

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

Preface

We have spent nearly three chapters on Pointer learning. We should all feel the power of pointer. Pointers can directly operate on data in the memory based on the address. If used properly, they not only reduce the amount of code, but also optimize memory management and improve program performance. There are still a lot of pointer content, such as pointer arrays, pointers to arrays, and pointers to pointers, however, I will not explain this content in my blog for the moment. I will only describe the most common usage of pointers in iOS development, such as the content in this chapter ----Returns the pointer function.AndPointer to function

 

1. functions that return pointers

Pointers are also a data type in C language, so the return value of a function can certainly be a pointer type.

The function that returns the pointer is generally in the following format:Type name * function name (parameter list)

For example, the following function returns a pointer to a char-type variable.

1 // convert the lowercase letters in the string 'str' into uppercase letters and return the changed string. 2 // note that the parameters here need to pass string variables, the character string constant 3 char * upper (char * str) {4 // The original address is reserved first. This is because the position indicated by str changes accordingly. 5 char * dest = str; 6 7 // if not empty character 8 while (* str! = '\ 0') {9 // if it is a lowercase letter 10 if (* str> = 'A' & * str <= 'Z ') {11 // change to uppercase letters. The ASCII values of lower case and upper case letters have a fixed difference of 12 * str-= 'a'-'A'; 13} 14 15 // traverse the next character 16 str ++; 17} 18 19 // return string 20 return dest; 21}

I will not explain the code in detail, but focus on the definition form of Line 1.

Calling this function is also very simple:

Int main () {// define a string variable char str [] = "lmj"; // call the function char * dest = upper (str); printf ("% s ", dest); return 0 ;}

Output result:

 

2. pointer to function

You may be surprised to see this title. If the pointer can point to a function, I will explain it first.Why can pointer point to a function??

1. Why can pointer point to a function??

As a program, a function also occupies part of the storage space in the memory. It also has a starting address, that is, the function entry address. If the function has its own address, it's easy. Our pointer variable is used to store the address. Therefore, you can use a pointer to point to a function. The function name represents the function address.

 

2. pointer to Function Definition

The general format of definition:Type of the return value of the function (* pointer variable name) (Form parameter 1, form parameter 2 ,...);

Note: variable names of formal parameters can be omitted, or even the entire list of formal parameters can be omitted.

1 # include <stdio. h> 2 3 int sum (int a, int B) {4 return a + B; 5} 6 7 int main () 8 {9 // define a pointer Variable p, point to the sum function 10 int (* p) (int a, int B) = sum; 11 // or int (* p) (int, int) = sum; 12 // or int (* p) () = sum; 13 14 // call the function 15 int result = (* p) (1, 3) using the pointer Variable p ); 16 // or int result = p (1, 3); 17 18 printf ("% d", result); 19 return 0; 20}

* First, a sum function is defined in row 3rd to receive two int-type parameters. The return value type is int.

* Then, a pointer Variable p pointing to the sum function is defined in row 10th. Note the definition form of p: int (* p) (int a, int B). The first int represents that the return value of the sum function is of the int type, and then * p is enclosed in parentheses () wrapped. The following int a and int B represent the form parameters of the sum function, which can be omitted completely. 10th rows, 11 rows, and 12 rows are feasible.

* In row 3, use * p to retrieve the pointing function, and then input the parameter to call the function. You can also use the 16th row method, which is no different from calling a common function.

The final output result is as follows:

 

3. Usage notes

1> since these pointer variables store the entry address of a function, it is meaningless to perform addition and subtraction operations (such as p ++) on them. Will p ++ point to the next function? Ridiculous !! No.

2> the definition of the function that returns the pointer char * upper (char * str) is very similar to the definition of the pointer to the function int (* p) (int a, int B, pay special attention to distinction during use

3> pointer variables pointing to functions are mainly used for two purposes:

  • Call a function

  • Pass functions as parameters between functions. I may not quite understand this. For example.

1 # include <stdio. h> 2 3 // subtraction operation 4 int minus (int a, int B) {5 return a-B; 6} 7 8 // addition operation 9 int sum (int, int B) {10 return a + B; 11} 12 13 // This counting function is used for calculation between a and B. As for addition or subtraction, 14 void counting (int (* p) (int, int), int a, int B) {15 int result = p (a, B) is determined by the 1st parameters of the function ); 16 printf ("Calculation result: % d \ n", result); 17} 18 19 int main () 20 {21 // addition calculation 22 counting (sum, 6, 4); 23 24 // perform the subtraction operation 25 counting (minus, 6, 4); 26 27 return 0; 28}

If you want to add a multiplication operation in the future, it is very simple. You don't need to modify the counting function code. You only need to add a multiplication operation function.

int mul(int a, int b) {    return a * b;}

Then counting (mul, 6, 4); can perform multiplication.

 

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.