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++的更多相关文章

  1. return array 评论添加状态和提示信息

    ThinkSNS漏洞系列第一弹,某处处理不当导致SQL注入 漏洞点出现在Comment Widget里:\addons\widget\CommentWidget\CommentWidget.class ...

  2. 取出return array() 数组内容

    d.php文件 return array( "0" => 内容一, "1" => 内容二, "2" => 内容三, &qu ...

  3. <?php return array(

    <?php //test.php return array( 'name' => 'andy', 'sex' => 'male' ); ?> <?php $set = r ...

  4. [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 ...

  5. 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 ...

  6. C++ std::array

    std::array template < class T, size_t N > class array; Code Example #include <iostream> ...

  7. 帝国备份王(Empirebak) \class\functions.php、\class\combakfun.php GETSHELL vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 EmpireBak是一款完全免费.专门为Mysql大数据的备份与导入而设 ...

  8. quick lua 3.3常用方法和学习技巧之functions.lua目录

    1.functions.lua (framework->functions.lua) 提供一组常用函数,以及对 Lua 标准库的扩展 1.printf 2.checknumber checkin ...

  9. js Array​.prototype​.reduce()

    例子: , , , ]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 ...

随机推荐

  1. uva 10154 - Weights and Measures【dp】qi

    题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包含自己的所有的重量. 分析:先按其能举起来的力量从小 ...

  2. 解决Mac OS下安装MyEclipse报错:Your system does not have sufficient memory to support MyEclipse

    最近想尝尝鲜,FQ去www.myeclipseide.com上下载了最新版的MyEclipse 15CI版,安装的时候,报告如下错误(MyEclipse 14也会出现这个问题): Your syste ...

  3. 在ASP.NET MVC中使用typeahead.js支持预先输入,即智能提示

    使用typeahead.js可以实现预先输入,即智能提示,本篇在ASP.NET MVC下实现.实现效果如下: 首先是有关城市的模型. public class City { public int Id ...

  4. Windows Phone本地数据库(SQLCE):10、创建数据库(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  5. sql语句中having的作用是?

    HAVING对由sum或其它集合函数运算结果的输出进行限制.比如,我们可能只希望看到Store_Information数据表中销售总额超过1500美圆的商店的信息,这时我们就需要使用HAVING从句. ...

  6. SpringBoot集成JWT实现token验证

    原文:https://www.jianshu.com/p/e88d3f8151db JWT官网: https://jwt.io/ JWT(Java版)的github地址:https://github. ...

  7. Java编解码分析

    一.为什么要编解码? 网络或磁盘传输的单位都是字节,平常我们使用的单位都是字符,所以数据需要在字节和字符之间进行转换. 二.编解码概念 1.编码:字符转换成字节 2.解码:字节转换成字符 三.常用字符 ...

  8. HTML5学习笔记:跨域

    在跨域安全性方面,有多个地方会有限制,主要是XMLHttpRequest对象的跨域限制和iFrame的跨域限制,下面我们分别来看一下. Ajax跨域(CORS) CORS是一个W3C标准,全称是&qu ...

  9. wdcp支持两种安装方式

    v3.2版本已发布,支持多PHP版本共存共用,支持SSL证书,更多可看论坛 v3版讨论区 更多安装说明请看 http://www.wdlinux.cn/bbs/thread-57643-1-1.htm ...

  10. Python数据分析笔记

    最近在看Python数据分析这本书,随手记录一下读书笔记. 工作环境 本书中推荐了edm和ipython作为数据分析的环境,我还是刚开始使用这种集成的环境,觉得交互方面,比传统的命令行方式提高了不少. ...