pointers:pointersin C + + are a special variable, and the values stored in it are interpreted as an address in memory , or the pointer is an object name in the programming language, and its value points to the memory address in the heap.
such as: int x=10 int *p=&x; The value of the pointer p defined at this point is 0xbfeb3204, and *p is the value of the memory address 10;
pointer variable: the variable that holds the address is called pointer variable, unlike the general variable, the general variable is the data itself, and the pointer variable is the address of the data;
First, Value delivery:
The normal function of the value of the way to pass, omitted here ...
Second, pointer passing:
The pointer variable is stored in the The address of a variable, the passing of a pointer in a function means that the function can find the actual storage address of the variable by the address pointed to by the pointer, thereby manipulating the contents of that address.
void swap (int *a,int *b) {
int temp=*a;
*a=*b;
*b=temp;
}
int main () {
int a=4;
int b=6;
cout<<a<< "" <<b<<endl;
Swap (&A,&B);
cout<<a<< "" <<b<<endl;
6 4
return 0;
}
Third, reference delivery
int x=ten; int &a=x;cout<<&a<<""//0xbfcb0850 int *b=&x;cout<<b<<""//0xbfcb0850 10
voidSwapint&a,int&b) { inttemp=A; A=b; b=temp;}intMain () {intA=Ten; intb= -; Swap (A, b); cout<<a<<" "<<b<<endl;//Ten return 0;}
C + + pointers