directory of this document
- Objective
- A function that returns a pointer
- Second, pointers to functions
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore
Back to the top of the preface
Before we spent nearly 3 chapters learning pointers, we should all feel the power of pointers. The pointer can manipulate the data in memory directly according to the address, so it can not only make the code less, but also optimize the memory management and improve the performance of the program. There are many things about pointers, such as pointer arrays, pointers to arrays, pointers to pointers, hehe, see if these names are big, but I will not be in the blog to explain these things, I only talk about the most common use of pointers in iOS development, such as the content of this chapter---- Functions that return pointers and pointers to functions
Back to top a function that returns a pointer
Pointers are also a type of data in the C language, so the return value of a function can certainly be a pointer type.
The general form of a function that returns a pointer is: type name * Function name (parameter list)
For example, the following function returns a pointer to a char type variable
1//Convert the lowercase letter in the string str to uppercase and return the changed string 2//Note that the argument here is to pass a string variable, cannot pass the string constant 3 char * UPPER (char *str) {4 ////////First preserve the original address. Because the position that STR points to will change. 5 char *dest = str; 6 7 //If it is not a null character 8 while (*str! = ')} {9 //If the lowercase letter is (*str >= ' a ' &am p;& *str <= ' z ') {one //change to uppercase. The ASCII value of lowercase and uppercase has a fixed difference of *str-= ' a '-' a '; }14 //traverse the next character str++;17 }18 19 //Returns a string of return dest;21}
I will not explain the code in detail, focus on the definition of the 2nd line of the form.
Calling this function is also very simple:
int main () { //define a string variable char str[] = "LMJ"; Call function char *dest = upper (str); printf ("%s", dest); return 0;}
Output Result:
Back to top two, pointers to functions
When you see this headline, you may be surprised that pointers can point to a function, so let me explain why pointers can point to a function .
1.
Why pointers can point to a function?
function as a program, in memory also occupy a portion of storage space, it also has a starting address, that is, the entry address of the function. function has its own address, that's good, our pointer variable is used to store the address. Therefore, a pointer can be used to point to a function. Where the function name represents the address of the function.
2. Definition of pointers to functions
The general form of the definition: The return value type of the function (* pointer variable name) (formal parameter 1, formal parameter 2, ...);
Note: The variable name of the formal parameter can be omitted, even the entire form parameter list 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 change The amount P, which points to the sum function of ten int (*p) (int a, int b) = sum;11 //or int (*p) (int, int) = sum;12 //or int (*p) () = sum;13
14 //Use pointer variable p to invoke a function with the result = (*p) (1, 3); +/ or int result = P (1, 3) ; printf ("%d", res ULT); return 0;20}
* First define a sum function in line 3rd, receive 2 parameters of type int, return value type int
* Then a pointer variable p that points to the SUM function is defined in line 10th. Note that the definition of P: Int (*p) (int a, int b), the 1th int represents the return value of the SUM function is of type int, then *p is wrapped in parentheses (), and the following int a and int b represent the parameters of the SUM function, which can be omitted altogether. Line 10th, line 11, and line 12 are all feasible
* In line 15th, first use *p to take out the function that points to, and then pass in the parameter call function. You can also take the approach in line 16th, which makes no difference to calling a normal function.
The final output: no surprises.
3. Use note
1> because this type of pointer variable stores the entry address of a function, it makes no sense to add or subtract them (such as p++). Is p++ going to point to the next function? Ridiculous!! There's no such thing.
2> the definition of a function that returns a pointer char *upper (char *str) and a pointer to a function that defines an int (*p) (int a, int b) is very similar, with particular attention to distinguishing
3> pointer variables that point to functions have two main uses:
1 #include <stdio.h> 2 3//subtraction operation 4 int minus (int a, int b) {5 Return a-C ; 6} 7 8//addition operation 9 int sum (i NT A, int b) {Ten return a + b;11}12 13//This counting function is used to do the calculation between A and B, as for addition or subtraction, the 1th parameter of the function determines the void counting (int (*p) ( int, int), int a, int b) {The result = P (A, b); printf ("evaluates to:%d\n", result);}18 int main () The addition operation is performed on the counting (sum, 6, 4), and the subtraction operation of counting (minus, 6, 4);
If you want to add a multiplication again later, it is very simple to not modify the code of the counting function, only to add a function of the multiplication operation.
int mul (int a, int b) { return a * b;}
Then counting (mul, 6, 4); you can do multiplication.
"C Language" 14-a pointer to a function that returns a pointer to a function