C + + pointers and reference issues

Source: Internet
Author: User

I was supposed to answer the question and take a note here.

The *&l is a reference to a pointer, and the argument is a pointer. So L is the alias of the argument pointer, and the modification of the alias L is equal to the modification of the argument.
*l is a pass value, you cannot change the value of the argument pointer variable passed in.
Program code:

#include <iostream>
using namespace Std;
void foo (int*p);
int main ()
{
int a=5;
int *b=&a;
printf ("%d%d\n", b,*b);
Foo (b);
printf ("%d%d", b,*b);
}
void foo (int* p)
{
*p=*p+1;
p=p+1;

}

Output to


Can be seen as pointer B has not changed, pointer b saved value changed
Program code:

#include <iostream>
using namespace Std;
void foo (int*&p);
int main ()
{
int a=5;
int *b=&a;
printf ("%d%d\n", b,*b);
Foo (b);
printf ("%d%d", b,*b);
}
void foo (int*& p)
{
*p=*p+1;
p=p+1;

}

The values saved by pointer B and pointer b are changed.
The following is a copy.
This technique is used for example to define a function that safely removes pointers, so to say, security is to delete the pointer only when it is zero, and then assign the pointer to null immediately after the deletion.
Template<typename t>
Inline Safe_delete (t*& ptr)
{
if (PTR)
{
Delete ptr;
ptr = NULL;
}
}
I use QT and vs both to find that the defined pointers are such security pointers.
The pointer is not zero and is not allowed to be deleted. deleted, if not again null.

C + + pointers and reference issues

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.