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 ...
随机推荐
- C#面向服务编程技术WCF从入门到实战演练
一.WCF课程介绍 1.1.Web Service会被WCF取代吗? 对于这个问题阿笨的回答是:两者在功能特性上却是有新旧之分,但是对于特定的系统,适合自己的就是最好的.不能哪一个技术框架和行业标准作 ...
- WebLogic使用总结(二)——WebLogic卸载
一.WebLogic 12c的卸载 WebLogic的卸载是非常容易的,找到WebLogic的卸载程序,如下图所示: 启动卸载程序,如下图所示:
- Linux内存管理学习3 —— head.S中的段页表的建立
作者 彭东林 pengdonglin137@163.com 平台 TQ2440 Qemu+vexpress-ca9 Linux-4.10.17 正文 继续分析head.S: ldr r13, =__m ...
- 新版ADT创建项目时出现appcompat_v7的问题
做Android开发的朋友最近会发现,更新ADT至22.6.0版本之后,创建新的安装项目,会出现appcompat_v7的内容.并且是创建一个新的内容就会出现.这到底是怎么回事呢?原来appcompa ...
- android中Bitmap的放大和缩小的方法
android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34 CSDN博客原文 http://blog.csdn.net/ada168855/article/det ...
- java.security.InvalidKeyException: Illegal key size aes解密失败
使用微信时定期提示:java.security.InvalidKeyException: Illegal key size和 com.qq.weixin.mp.aes.AesException: ae ...
- 同一个账号启动两个resin而要求使用不同jdk的解决方法
天,一个老同事问起这样一个问题,起因是他们的系统由于某些原因原有的部分模块不能运行在新的jdk下,所以需要启动两个resin并使用不同jdk.由 于是要开机自动启动,因此启动的账号还需要是同一个.这样 ...
- SQL中树形分层数据的查询优化
在数据查询中,从2008开始SQL Server提供了一个新的数据类型hierarchyid,专门用来操作层次型数据结构. hierarchyid 类型对层次结构树中有关单个节点的信息进行逻辑编码的 ...
- .Net Standard简介
.NET Standard 是一套正式的 .NET API 规范,有望在所有 .NET 运行时中推出. 推出 .NET Standard 的背后动机是要提高 .NET 生态系统中的一致性. ECMA ...
- main函数的参数argc和argv
版权声明:本文为博主原创文章,转载请注明CSDN博客源地址!共同学习,一起进步~ https://blog.csdn.net/Eastmount/article/details/20413773 该篇 ...