C/C++ 指针的使用
2015-11-18 16:01
399 查看
#include<iostream>
using namespace std;
int main()
{
int *p =new int(2);
int *q = new int(3);
cout<<"before p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
p = q; //p没有使用它本身分配的内存而是指向q
cout<<"after p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
delete q; //删除后q指向的内存被释放
cout<<"after detete q (but not q=NULL)"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
q = NULL;
return 0;
}
运行结果如下:
using namespace std;
int main()
{
int *p =new int(2);
int *q = new int(3);
cout<<"before p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
p = q; //p没有使用它本身分配的内存而是指向q
cout<<"after p = q"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
delete q; //删除后q指向的内存被释放
cout<<"after detete q (but not q=NULL)"<<endl<<"address of p and value:"<<p<<" "<<*p<<endl<<"address of q and value:"<<q<<" "<<*q<<endl;
q = NULL;
return 0;
}
运行结果如下:
相关文章推荐
- Effective C++ 笔记
- C++拷贝构造函数详解
- C++ 显式调用DLL
- C++如何巧妙定义宏
- c/c++中main方法中的return值 & 参数 & 剪切
- VC++向txt文件中写入字符串
- C++标准库
- C语言中强制类型转换的总结
- 状态机的C语言编程(转)
- Decorator(装饰)设计模式
- vc++向txt文件中写入数据,追加数据
- C++堆栈区别
- 深入C语言返回值
- C++(class)总结
- inux 下c/c++ 连接mysql数据库全过程-----已经通过验证
- C语言连接数据库
- C语言获取时间字符串
- C语言连接MySql数据库
- c++封装多线程类
- 如何用c语言产生一定范围内的随机数?