Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Six pointers and functions
01 Pointers as Function parameters
When a pointer is an argument, as with a normal variable, it is also a one-way value pass that passes the value of the pointer variable (the value is an address) to the parameter of the modulated function (which must also be a pointer variable). Because the value passed by the formal parameter acceptance argument is an address, the two points to the same variable.
* If you want the C language function call to implement multiple value changes, you can design a formal parameter is a pointer variable of the function, so that the principal function of the argument pointer and the function of the parameters of the parameter pointer, according to the principle of the parameter pointer and the actual parameter pointer to the same address, you can change the value of the address, The transfer of data between the keynote function and the tuned function is implemented, so that the function actually returns multiple values to the keynote function.
//enter two integers, use a custom function to swap the two values, and output the swapped results in the main function.#include <stdio.h>voidSwapint*x,int*y);//function DeclarationintMain () {intb;//define two integers that need to be exchanged int*P1,*P2;//defines two pointers to type intprintf ("Please enter two number: \ n");//Enter a two integerscanf"%d%d", &a,&b);//Accept two integersprintf ("a=%d,b=%d\n before Exchange", A, b);//integer before output interchangeP1= &a;//so that P1 points to X.P2 = &b;//so that P2 points to Y.Swap (P1,P2);//Call function interchange A and Bprintf"after Exchange: a=%d,b=%d\n", A, b); }voidSwapint*x,int*y) { inttemp; Temp= *x; *x = *y; *y =temp;}
02 functions that return pointer values
General form:
Data type * Function name (formal parameter list)
{
function body;
}
03 pointer variables to functions
You can assign the first address of a function to a pointer variable, make the pointer variable point to the function, and then find and call the function with a pointer variable called the function pointer variable.
General form:
Data type (* pointer variable name) (function parameter list)
#include <stdio.h>intMain () {intA =4474; int*p = &A; //p = &a;*p =2; printf ("%d\n",*p); //Variable type variable name; //format: Variable type * variable name; //defines a pointer variable p//pointer variables can only store addresses//The pointer is a function: can access the corresponding storage space according to an address value//int before pointer variable p: Pointer variable p can only point to data of type int return 0;}
12-Dark Horse programmer------C language Learning notes---C language pointers and functions