C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example:

int
* myFunction()
{

.

.

.

}

Second point to remember is that C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.

Now, consider the following function, which will generate 10 random numbers and return them using an array and call this function as follows:

#include
<iostream>

#include
<ctime>

 

using
namespace std;

 

// function to generate and retrun random numbers.

int
* getRandom(
)
{

static
];

 

// set the seed

srand(
(unsigned)time( NULL )
);

    
 

for
(int i =
; i <
;
++i)
{

r[i]
= rand();

cout << r[i]
<< endl;

}

 

return r;

}

 

// main function to call above defined function.

int main ()
{

// a pointer to an int.

int
*p;

 

p = getRandom();

    
 

for
(
int i =
; i <
; i++
)
{

cout <<
"*(p + "
<< i <<
") : ";

cout <<
*(p + i)
<< endl;

}

 

return
;

}

When the above code is compiled together and executed, it produces result something as follows:

624723190

1468735695

807113585

976495677

613357504

1377296355

1530315259

1778906708

1820354158

667126415

*(p + 0) : 624723190

*(p + 1) : 1468735695

*(p + 2) : 807113585

*(p + 3) : 976495677

*(p + 4) : 613357504

*(p + 5) : 1377296355

*(p + 6) : 1530315259

*(p + 7) : 1778906708

*(p + 8) : 1820354158

*(p + 9) : 667126415

最新文章

  1. ajax请求和aspx返回数据
  2. Babel 学习
  3. .NET平台下开源框架
  4. 二、SQL语句映射文件(2)增删改查、参数、缓存
  5. python中的类型转换
  6. 小白日记10:kali渗透测试之端口扫描-UDP、TCP、僵尸扫描、隐蔽扫描
  7. POJ 1061 青蛙的约会(扩展欧几里德)
  8. ASP长文章分页的两个方法,函数
  9. Linux下使用QQ的几种方式
  10. [TYVJ] P1001 第K极值
  11. serialVersionUID行动
  12. EasyUI DataGrid 基于 Ajax 自定义取值(loadData)
  13. ubuntu 使用sudo apt-get update 出现 被配置多次导致无法升级错误解决方法
  14. LindDotNetCore~Polly组件对微服务场景的价值
  15. python_为被装饰的函数保留元数据
  16. 学习笔记7-Android短信发送器
  17. 线程ava.lang.OutOfMemoryError: unable to create new native thread
  18. mysql 分库分表 ~ ShardingSphere生态圈
  19. 使用Navicat为MySQL建立定时任务
  20. ICO和IPO

热门文章

  1. 《Go语言实战》摘录:7.3 并发模式 - work
  2. Plan Explorer数据库
  3. SVN的管理方式和git的管理方式
  4. 在Visual Studio中使用类图描述领域模型
  5. 关于面试总结10-selenium中隐藏元素如何定位?(hidden、display: none)
  6. centos7安装debuginfo
  7. SVN更新的问题
  8. Android:客户端和服务器之间传输数据加密
  9. ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)
  10. c#录音和放音,超简单!不用DirectX