C + + pointers

Source: Internet
Author: User

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

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.