指针杂篇
来源:互联网 发布:代理服务器软件 开源 编辑:程序博客网 时间:2024/05/16 00:31
1、返回指针(数组首地址)的函数中,因为C++ 不支持在函数外返回局部变量的地址,除非定义局部变量为 static 变量。所以代码如下:
#include <iostream>#include <ctime>#include <cstdlib>using namespace std;// 要生成和返回随机数的函数int * getRandom( ){ static int r[10];//注意把这个数组定义为static类型。 // 设置种子 srand( (unsigned)time( NULL ) ); for (int i = 0; i < 10; ++i) { r[i] = rand(); cout << r[i] << endl; } return r;}// 要调用上面定义函数的主函数int main (){ // 一个指向整数的指针 int *p; p = getRandom(); for ( int i = 0; i < 10; i++ ) { cout << "*(p + " << i << ") : "; cout << *(p + i) << endl; } return 0;}
2、
常量引用
int i = 42; const int &r1 = i;cout<<r1;
这个 输出的是i里面的值,42。
常量指针
int i = 42; const int *ptr;ptr=&i;
需要通过:
cout<<*ptr;
来输出
阅读全文
0 0
- 指针杂篇
- 指针篇
- 指针篇之十一 函数指针
- C++新手入门--指针篇
- C指针初级篇
- 面试题---指针篇
- C++指针篇
- 指针---入门篇
- 指针下篇,接上篇
- 第四篇 高级指针
- C++ 指针篇01
- 【指针篇】C语言:指针与数组的区别,数组指针与指针数组的剖析。
- 指针
- 指针
- 指针
- 指针
- 指针
- 指针
- spring拦截器实现demo
- 欢迎使用CSDN-markdown编辑器
- 数据嗨客 | 第4期:逻辑回归
- Spring MVC中AOP无效、不起作用,解决方案
- HDU-6188 Duizi and Shunzi(思维)
- 指针杂篇
- three.js 天空盒说明
- B
- 阿里云(一)tomcat环境搭建
- 快速构建Spring Boot 1.5.8 maven Web 项目
- 是学习Java还是Python?一张图告诉你!
- android 集成高德地图打包后报 key-location Error, ErrCode:7 的问题
- 反转字符串中的单词
- easyui-combox 下拉框选择默认值