C++指针理解《一》
2016-04-02 13:36
351 查看
贴个代码,注释比较清楚了。对于局部指针变量的问题,参见http://ask.csdn.net/questions/246619,感谢这么多热心人回答这个问题。
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
const int* pointParmas(const int *t);
int*& pointBoom();
int main()
{
int*a=NULL;
int d = 10;
int &c = d;//c是引用
cout << "d的值" << d<<endl;
cout << "d的地址" << &d<<endl;
cout << "c的值" << c<<endl;
cout << "c的地址" << &c<<endl;
a = &d;
cout << "实参a的值" << *a << endl;
cout << "实参a中存储的地址" << a << endl;
cout << "实参a的地址" << &a << endl;
cout << "调用函数pointParmas" << endl;
cout << *pointParmas(a)<<endl;
cout << "d的值" << d<<endl;
cout << "实参a的中存储地址" << a << endl;
cout << "实参a的地址" << &a << endl;
cout << "调用函数pointBoom" << endl;
cout << "pointBoom的值是" << *pointBoom() << endl;
cout << "pointBoom中返回的地址是" << pointBoom() << endl;
int s = 0;
cin >> s;
return 0;
}
const int* pointParmas(const int* t) //指针存储的是内存地址
{
cout << "形参t的中存储的地址" << t<<endl;
cout << "形参t的地址" << &t << endl;
int c = 20;
cout << "c的地址" << &c<<endl;
t =&c;
//*t = 30//编译时报错,左值必须可以修改,形参t是常量指针,const*int p这种类型,属于能修改指针指向的地址,但是不能修改指针指向的值
//补充int *const p能修改指针指向的值,但是不能改地址,在这个函数中,如果t属于这种类型旳指针,那么t&g就会报错
//const int *const p是最严格,既不能改值也不能改地址
//int g = 30;
//t = &g;
cout << "形参t中存储的地址" << t<<endl;
cout << "形参t的地址" << &t << endl;
return t;
}
int*& pointBoom()//局部变量指针测试
{
int c = 20;
int *f = &c;//局部变量指针f
cout << "局部变量c的地址" << &c<<endl;
cout << "局部变量f存储的地址" << f<<endl;
cout << "局部变量f的地址" << &f<<endl;
return f;
}
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
const int* pointParmas(const int *t);
int*& pointBoom();
int main()
{
int*a=NULL;
int d = 10;
int &c = d;//c是引用
cout << "d的值" << d<<endl;
cout << "d的地址" << &d<<endl;
cout << "c的值" << c<<endl;
cout << "c的地址" << &c<<endl;
a = &d;
cout << "实参a的值" << *a << endl;
cout << "实参a中存储的地址" << a << endl;
cout << "实参a的地址" << &a << endl;
cout << "调用函数pointParmas" << endl;
cout << *pointParmas(a)<<endl;
cout << "d的值" << d<<endl;
cout << "实参a的中存储地址" << a << endl;
cout << "实参a的地址" << &a << endl;
cout << "调用函数pointBoom" << endl;
cout << "pointBoom的值是" << *pointBoom() << endl;
cout << "pointBoom中返回的地址是" << pointBoom() << endl;
int s = 0;
cin >> s;
return 0;
}
const int* pointParmas(const int* t) //指针存储的是内存地址
{
cout << "形参t的中存储的地址" << t<<endl;
cout << "形参t的地址" << &t << endl;
int c = 20;
cout << "c的地址" << &c<<endl;
t =&c;
//*t = 30//编译时报错,左值必须可以修改,形参t是常量指针,const*int p这种类型,属于能修改指针指向的地址,但是不能修改指针指向的值
//补充int *const p能修改指针指向的值,但是不能改地址,在这个函数中,如果t属于这种类型旳指针,那么t&g就会报错
//const int *const p是最严格,既不能改值也不能改地址
//int g = 30;
//t = &g;
cout << "形参t中存储的地址" << t<<endl;
cout << "形参t的地址" << &t << endl;
return t;
}
int*& pointBoom()//局部变量指针测试
{
int c = 20;
int *f = &c;//局部变量指针f
cout << "局部变量c的地址" << &c<<endl;
cout << "局部变量f存储的地址" << f<<endl;
cout << "局部变量f的地址" << &f<<endl;
return f;
}
相关文章推荐
- [leetcode ]221. Maximal Square c语言
- 多态在 Java 和 C++ 编程语言中的实现比较
- C++-优先级队列
- 【C语言】编写一个函数实现n^k,使用递归实现
- C语言(自加自减)
- C\C++ 获取当前路径
- c++中的构造函数和拷贝构造函数
- C语言中Union的用法
- c++学习(2.2)变量
- C语言函数和汇编函数相互调用
- 几个有关iOS的几个常见问题-----为什么选择OC语言
- 设计类CDate以满足:输出年月日日期格式;输入的日期加1;设置日期(参考清华版李春葆C++书籍)
- C++中的输出格式 八进制 十进制 十六进制
- c++断言设置
- C++中构造函数或析构函数定义为private
- 第1周 C语言及程序设计初步 例程-7 问题求解方法——迭代
- C语言18个经典问题答录
- C++ 判断double变量是否为0
- [leetcode]326. Power of Three(c语言)
- 测试某一段程序运行时间的方法