Use of pointers and pointers in C + + reference

Source: Internet
Author: User

When the pointer is passed as a function parameter, it is essentially a placement pass, a copy of the pointer is made, and the modification of the pointer within the function is actually a modification of the local variable inside the function. This is different from the reference, in fact, when the argument is passed, the address of the actual variable is transmitted, and when the variable is accessed inside the function, it is actually done in the way of indirect access, so it actually accesses the meta variable. However, because the address is only copied, the changes to the address pointed to by the pointer do not affect the original pointer. If you want to implement a pointer modification, you need to pass the pointer's pointer or the application of the pointer.

A pointer to a pointer

int value=3;void func (int **p) {    *p = &value;} int main (int argc, char *argv[]) {    int n = 1;    int *PN = &n;    cout << *pn << Endl;    Func (&PN);    cout << *PN <<endl;    return 0;}

int *p is the address of the address, that is, p points to a memory space, where an address is placed. If we pass the value and pass it directly to the function, then the internal copy does not change the P itself. Similar to the outside of the function is an int *p, inside is an int * tmpp, both inside the content is the same, that is the real address, but the address of the two itself is not the same, the changes to TMPP will not affect the p. You can do this by using pointers to pointers. int **p; In the function, the first solution to the reference, actually get the true address of P, so that p itself can be modified.

Ii. references to pointers

int value=3;void func (int *&p) {    p = &value;} int main (int argc, char *argv[]) {    int n = 2;    int *PN = &n;    cout << *pn << Endl;    Func (PN);    cout << *PN <<endl;    return 0;}

Take a look at int *&p actually, the essence P is a reference, a reference to a pointer, so the modification to P is actually a modification of the pointer. Here in the new understanding pointer int *p, essentially address, that is, the P pointer is placed in an address, itself P also has an address. So when the int *& p is initialized then the address that the P points to will not change, that is, the memory space of the storage address. But the contents of this can be changed, this is the feature of reference, here is actually the address can be changed.

Use of pointers and pointers in C + + reference

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.