c++中指针,指针的引用,指针的指针的区别
来源:互联网 发布:2017软件研发费用 编辑:程序博客网 时间:2024/04/29 23:54
看例子和结果:
结果:
comments:
对p1指针:
cout<<"---------------------------------------"<<endl;
int *p1 = new int;
*p1 = 1;
cout<<"*p1="<<*p1<<endl;
// FreePtr1(p1);
void FreePtr1(int* ptr1)
{
delete ptr1;
ptr1 = NULL;
}
cout<<"after call freePtr1"<<endl;
if(p1 != NULL)
{
cout<<"p1 is not null"<<endl;
cout<<"*p1="<<(*p1)<<endl;
}
cout<<"---------------------------------------"<<endl;
而p2为:
调用前:
调用后:
注意:函数的参数进行值拷贝,即使传的是指针,也的对指针(即指针里存的地址)的拷贝, 可不是指针里地址所指的值的拷贝啊!
#include "stdafx.h"
#include <iostream>
using namespace std;
void FreePtr1(int* ptr1)
{
delete ptr1;
ptr1 = NULL;
}
void FreePtr2(int*& ptr2)
{
delete ptr2;
ptr2 = NULL;
}
void FreePtr3(int **ptr3)
{
delete *ptr3;
*ptr3 = NULL;
}
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");
}
#include <iostream>
using namespace std;
void FreePtr1(int* ptr1)
{
delete ptr1;
ptr1 = NULL;
}
void FreePtr2(int*& ptr2)
{
delete ptr2;
ptr2 = NULL;
}
void FreePtr3(int **ptr3)
{
delete *ptr3;
*ptr3 = NULL;
}
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");
}
结果:
comments:
对p1指针:
cout<<"---------------------------------------"<<endl;
int *p1 = new int;
*p1 = 1;
cout<<"*p1="<<*p1<<endl;
// FreePtr1(p1);
void FreePtr1(int* ptr1)
{
delete ptr1;
ptr1 = NULL;
}
cout<<"after call freePtr1"<<endl;
if(p1 != NULL)
{
cout<<"p1 is not null"<<endl;
cout<<"*p1="<<(*p1)<<endl;
}
cout<<"---------------------------------------"<<endl;
而p2为:
调用前:
调用后:
注意:函数的参数进行值拷贝,即使传的是指针,也的对指针(即指针里存的地址)的拷贝, 可不是指针里地址所指的值的拷贝啊!
- C++中引用,指针,指针的引用,指针的指针
- C++中引用,指针,指针的引用,指针的指针
- 【C++】指针&引用的区别
- 指针,指针的指针,指针的引用的区别
- C中指针与引用的区别
- c中指针与引用的区别
- 指针 指针的引用 指针的指针
- 指针、指针的引用
- 指针,引用 的区别
- 引用 指针的区别
- c中指针的指针
- 引用、指针的指针、指针的引用
- c++中指针,指针的引用,指针的指针的区别
- c++中指针,指针的引用,指针的指针的区别
- c++中指针,指针的引用,指针的指针的区别
- 指针的引用与指针的区别。
- 引用,指针,常量引用,常量指针,指针常量的区别
- C++中引用、指针的指针、指针的引用解析
- 小公司之所以小
- 保护你的DLL和Code不被别人使用 -C#篇
- 我在网站zegeniestudios做的测试
- jdbc学习(转载)
- 《Beyond Java》(一)
- c++中指针,指针的引用,指针的指针的区别
- 简单的模板实例,实现简单的运算符重载
- System.IO.Compression下的2个类搞了好久,原因看注释
- vc 学习点滴之终结篇
- 基于.net cf C# 实现杀进程
- VS2005中检测和更正 C/C++ 代码缺陷
- Microsoft SQL Server 数据库自动部署工具
- 如何调试程序的 Release 版本
- 《Linux内核完全注释》笔记(1)