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. js判断只能输入数字或小数点

    JS判断只能是数字和小数点 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'' ...

  2. DTrace Probes in HotSpot VM

    http://docs.oracle.com/javase/6/docs/technotes/guides/vm/dtrace.html

  3. poj1050最大子矩阵和

    这篇是看了别人的报告写的,就当是屡屡思路好了. 题目大意.给定一个n阶矩阵(方阵),每一个元素中存在一个数字.任务就是求出一个最大的子矩阵使得矩阵元素之间的和是最大的. n=100; 1.矩阵A[m] ...

  4. 在ASP.NET MVC中使用Knockout实践04,控制View Model的json格式内容

    通常,需要把View Model转换成json格式传给服务端.但在很多情况下,View Model既会包含字段,还会包含方法,我们只希望把字段相关的键值对传给服务端. 先把上一篇的Product转换成 ...

  5. 分享一段ios数据库代码,包括对表的创建、升级、增删查改

    分享一段ios数据库代码.包括创建.升级.增删查改. 里面的那些类不必细究,主要是数据库的代码100%可用. 数据库升级部分,使用switch,没有break,低版本一次向高版本修改. // DB.h ...

  6. 【mysql】update的in的嵌套查询更新,如果字段中包含字符串A,统一替换为字符串B

    对于select的in嵌套子查询[DQL]: select en_name from goods where uid in( select uid from goods where goods_typ ...

  7. EntityFramework:状态变化与方法的关系(朋友的学习笔记)

    一.约定OnModelCreating 有一些限制需要注意,例如: 1.表名不支持使用标签进行标注 2.最小长度在 OnModelCreating 中不支持 3.正则表达式在 OnModelCreat ...

  8. linux下vi操作Found a swap file by the name

    当我在linux下用vi打开Test.java文件时 [root@localhost tmp]# vi Test.java 会出现如下信息: E325: ATTENTION Found a swap  ...

  9. Android5.0 ListView特效的简单实现

    Android5.0中对于动画可所谓是情有独钟,在设计规范中大量展现了listview的动画,其实也就是一个目的:将items动画显示出来.这个看起来很炫的效果,其实实现也蛮简单的,我下面就来用动画简 ...

  10. Pandas 快速入门(二)

    本文的例子需要一些特殊设置,具体可以参考 Pandas快速入门(一) 数据清理和转换 我们在进行数据处理时,拿到的数据可能不符合我们的要求.有很多种情况,包括部分数据缺失,一些数据的格式不正确,一些数 ...