c++中指针,指针的引用,指针的指针的区别
看例子和结果:
#include "stdafx.h"
#include <iostream>
using namespace std;
void FreePtr1(int* ptr1)
{
delete ptr1;
ptr1 = NULL;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
void FreePtr2(int*& ptr2)
{
delete ptr2;
ptr2 = NULL;
}
void FreePtr3(int **ptr3)
{
delete *ptr3;
*ptr3 = NULL;
}
![](//www.cppblog.com/Images/OutliningIndicators/None.gif)
void main()
{
cout<<"---------------------------------------"<<endl;
int *p1 = new int;
*p1 = 1;
cout<<"*p1="<<*p1<<endl;
FreePtr1(p1);
cout<<"after call freePtr1"<<endl;
if(p1 != NULL)
{
cout<<"p1 is not null"<<endl;
cout<<"*p1="<<(*p1)<<endl;
}
cout<<"---------------------------------------"<<endl;
int *p2 = new int;
*p2 = 2;
cout<<"*p2="<<*p2<<endl;
FreePtr2(p2);
cout<<"after call freePtr2"<<endl;
if(p2 != NULL)
{
cout<<"*p2="<<*p2<<endl;
}
else
{
cout<<"the p2 is null"<<endl;
}
cout<<"---------------------------------------"<<endl;
int *p3 ;
p3 = new int(3);
cout<<"*p3="<<*p3<<endl;
FreePtr3(&p3);
cout<<"after call freePtr3"<<endl;
if(p3 != NULL)
{
cout<<"*p3="<<*p3<<endl;
}
else
{
cout<<"the p3 is null"<<endl;
}
cout<<"---------------------------------------"<<endl;
system("pause");
![](//www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
结果:
![o_cpppointer.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_cpppointer.jpg)
comments:
对p1指针:
cout<<"---------------------------------------"<<endl;
int *p1 = new int;
*p1 = 1;
cout<<"*p1="<<*p1<<endl;
![o_p1before.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_p1before.jpg)
// FreePtr1(p1);
void FreePtr1(int* ptr1)
{
![o_ptr1before.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1before.jpg)
delete ptr1;
![o_ptr1delete.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1delete.jpg)
ptr1 = NULL;
![o_ptr1null.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1null.jpg)
}
![o_p1after.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_p1after.jpg)
cout<<"after call freePtr1"<<endl;
if(p1 != NULL)
{
cout<<"p1 is not null"<<endl;
cout<<"*p1="<<(*p1)<<endl;
}
cout<<"---------------------------------------"<<endl;
而p2为:
调用前:
![o_p2before.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_p2before.jpg)
调用后:
![o_p2after.jpg](//www.cppblog.com/images/cppblog_com/mzty/2021/o_p2after.jpg)
注意:函数的参数进行值拷贝,即使传的是指针,也的对指针(即指针里存的地址)的拷贝, 可不是指针里地址所指的值的拷贝啊!
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
结果:
![o_cpppointer.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_cpppointer.jpg)
comments:
对p1指针:
cout<<"---------------------------------------"<<endl;
int *p1 = new int;
*p1 = 1;
cout<<"*p1="<<*p1<<endl;
![o_p1before.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_p1before.jpg)
// FreePtr1(p1);
void FreePtr1(int* ptr1)
{
![o_ptr1before.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1before.jpg)
delete ptr1;
![o_ptr1delete.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1delete.jpg)
ptr1 = NULL;
![o_ptr1null.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_ptr1null.jpg)
}
![o_p1after.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_p1after.jpg)
cout<<"after call freePtr1"<<endl;
if(p1 != NULL)
{
cout<<"p1 is not null"<<endl;
cout<<"*p1="<<(*p1)<<endl;
}
cout<<"---------------------------------------"<<endl;
而p2为:
调用前:
![o_p2before.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_p2before.jpg)
调用后:
![o_p2after.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/o_p2after.jpg)
注意:函数的参数进行值拷贝,即使传的是指针,也的对指针(即指针里存的地址)的拷贝, 可不是指针里地址所指的值的拷贝啊!
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!