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
int r[10];

?

// set the seed

srand(
(unsigned)time( NULL )
);

????
?

for
(int i =
0; i <
10;
++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 =
0; i <
10; i++
)
{

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

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

}

?

return
0;

}

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

时间: 2024-08-03 20:52:48

Return array from functions in C++的相关文章

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

ThinkSNS漏洞系列第一弹,某处处理不当导致SQL注入 漏洞点出现在Comment Widget里: \addons\widget\CommentWidget\CommentWidget.class.php:138/*** 添加评论的操作** @return array 评论添加状态和提示信息*/public function addcomment() {// 返回结果集默认值$return = array ('status' => 0,'data' => L ( 'PUBLIC_CONC

php 显示php 中数组return Array

php 读取文本中数组,修改指定数组键值.达到统计下载文件排行. <?php header("Content-type:text/html;charset=utf-8");if(isset($_GET['down_id']) && !empty($_GET['down_id'])){ $down_id = $_GET['down_id']; $file='down_id.php'; $du = require_once($file); arsort($du); i

php获取文件 return array数组的值

array.php的代码如下<?php    return    array("a"=>"1");?> 然后再创建一个echo.php,代码如下<?php    $indb=include("array.php");    print_r($indb);?>

php 获取一个文件中return array() 的值

<?php return array( 'name' => 'andy', 'sex' => 'male' ); ?> <?php $set = include("test.php"); print_r($set);exit; ?>

[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, but they can incur performance problems. var ary = [1,2,3,4,5,6]; var res = ary.filter(function(x, i, arr){ console.log("filter: " + x); console.lo

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 those of the template arguments. Here is an example, mul(T a, T b) is a function template that calculates the product of a and b. Arguments a and b ar

01 - Execise About Array.prototype.reduce()

Description: Write a generic function chainer Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby). The input for each function is the output of the previous function (ex

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

catalog 1. 漏洞描述 2. 漏洞触发条件 3. 漏洞影响范围 4. 漏洞代码分析 5. 防御方法 6. 攻防思考 1. 漏洞描述 EmpireBak是一款完全免费.专门为Mysql大数据的备份与导入而设计的软件,系统采用分卷备份与导入,理论上可备份任何大小的数据库,帝国备份王(Empirebak)存在较多GETSHELL漏洞,本文逐一讨论从进入后台到GETSHELL的各种方式 Relevant Link: http://help.aliyun.com/knowledge_detail.

Understand JavaScript Callback Functions and Use Them

In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be used in a first-class manner like any other object (String, Array, Number, etc.) since they are in fact objects themselves. They can be “store