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 ...
随机推荐
- hdu5094 Maze
--就是爬管道-- 还好内存给的多-- 不然就不会做了-- #include<iostream> #include<map> #include<string> #i ...
- VirtualBox 在WIN7 X64 安装报错 获取VirtualBox COM对象失败,Unable to start the virtual device
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\CLSID\{---C000-}\InprocServer32] @="C:\ ...
- JavaScript进阶系列02,函数作为参数以及在数组中的应用
有时候,把函数作为参数可以让代码更简洁. var calculator = { calculate: function(x, y, fn) { return fn(x, y); } }; var su ...
- TXMLDocument use case (Delphi)
Description This example illustrates the basic operations on an XML document. Code procedure CreateD ...
- 11i and R12 Table Count in Different Module
Advertisement Module 11i Tables R12 Tables New Tables AR 551 616 118 BOM 264 337 73 GL 186 309 140 A ...
- C#实现缩放字体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 女子监狱第四季/全集Orange Is the New Black迅雷下载
女子监狱 第三季 Orange Is the New Black 3 (2015) 本季看点:该剧由<吉尔莫女孩>.<单身毒妈第一季>编剧杰姬·科恩的打造.由<护士当家& ...
- 【.Net】 C#访问修饰符
一 类的修饰符: C#中类的默认修饰符是internal.1 private 只有对包.NET中的应用程序或库才能访问.2 public 不限制对类的访问. 3 protected 只可以被本类和其 ...
- 多线程-Executors和Executor,线程池
jdk1.5之前,所有的线程都是需要自己手动创建的,由jvm销毁,当请求过多的时候,频繁的创建和销毁线程是非常浪费资源的.jdk1.5为此做了优化,提供了 java.util.concurrent 包 ...
- eclipse 创建聚合maven项目
本人不想花太多时间去排版,所以这里排版假设不好看,请多多包涵! 一直都在用maven,可是却基本没有自己创建过maven项目,今天也试着创建一个. 1.打开eclipse.然后new,other,然后 ...