Return array from functions in C++
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
Return array from functions in C++的更多相关文章
- return array 评论添加状态和提示信息
ThinkSNS漏洞系列第一弹,某处处理不当导致SQL注入 漏洞点出现在Comment Widget里:\addons\widget\CommentWidget\CommentWidget.class ...
- 取出return array() 数组内容
d.php文件 return array( "0" => 内容一, "1" => 内容二, "2" => 内容三, &qu ...
- <?php return array(
<?php //test.php return array( 'name' => 'andy', 'sex' => 'male' ); ?> <?php $set = r ...
- [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
Higher order Array functions such as filter, map and reduce are great for functional programming, bu ...
- c++11: trailing return type in functions(函数返回类型后置)
In C++03, the return type of a function template cannot be generalized if the return type relies on ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
- 帝国备份王(Empirebak) \class\functions.php、\class\combakfun.php GETSHELL vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 EmpireBak是一款完全免费.专门为Mysql大数据的备份与导入而设 ...
- quick lua 3.3常用方法和学习技巧之functions.lua目录
1.functions.lua (framework->functions.lua) 提供一组常用函数,以及对 Lua 标准库的扩展 1.printf 2.checknumber checkin ...
- js Array.prototype.reduce()
例子: , , , ]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 ...
随机推荐
- FileReader读取中文txt文件编码丢失问题(乱码)(转)
有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文 ...
- POJ 1386 Play on Words (有向图欧拉路径判定)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8768 Accepted: 3065 Des ...
- DWZ(JUI) 教程 跨域请求 iframeNavTab
如果想navTab访问其他的网址,可以使用 iframe navTab 使用时也非常简单 <li><a href="http://www.baidu.com" ...
- 【优化】COUNT(1)、COUNT(*)、COUNT(常量)、COUNT(主键)、COUNT(ROWID)等
http://blog.itpub.net/26736162/viewspace-2136339/
- [置顶] js 实现 <input type="file" /> 文件上传
在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑.浏览的字样不能换,我们一般会用让,<input type ...
- [vs2013]远程服务器调试
摘要 有时遇到比较奇葩的问题,比如本地程序正常运行,在服务器上不可以,如果日志也不起作用,那么远程调试就非常必要了,在服务器上安装vs,是比较费力费时的. 步骤 1.找到vs安装目录,一般默认安装在c ...
- React和Vue特性和书写差异
Vue均使用ES6语法,主要以单文件组件为例,写法上优先使用缩写. React使用TS语法. 生命周期 Vue React 入口&根实例 Vue const app = new Vue({ / ...
- AngularJS路由系列(1)--基本路由配置
本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● 路由的Big Picture ● $routeProvider配置路由 ● 使用template属性 ● 使用temp ...
- 内存溢出导致jenkins自动部署到tomcat失败
原文地址:http://openwares.net/java/jenkens_deploy_to_tomcat_error_of_outofmemoryerror.html jenkins自动部署wa ...
- xcode调试查看变量的值
对于IPhone开发/XCode的初学者,如何在调试时查看变量的值是很头痛的事情.因为Xcode的expression 经常无法正确显示变量的值.但是强大的GDB可以很方便的帮我们查看变量的值. 当执 ...