Return array from functions in C++
2017-07-22 13:42
330 查看
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
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
相关文章推荐
- How to pass in/out return VB Byte array from a COM Component written in C#
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Leetcode_find-minimum-in-rotated-array(c++ version)
- LeetCode 之 Kth Largest Element in an Array — C++ 实现
- 【leetcode c++】80 Remove Duplicates from Sorted Array II
- How Would You Get the Count of an Array in C++?
- From CString to char * in C++ with unicode setting
- [C++]LeetCode: 2 Remove Duplicates from Sorted Array
- c++ friend functions and inline functions
- Inline Functions in C++
- OpenCV c++ assertion failed <i < 0> in cv::_InputArray::getMat 错误的原因
- Inline Functions (C++)
- 【LeetNode2-1-1】Remove duplicates from sorted array I--C++源代码(VS2015)
- 【leetcode】Array——Construct Binary Tree from Preorder and Inorder Traversal(105)
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
- Some interesting facts about static member functions in C++
- LeetCode刷题【Array】 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode刷题(C++)——Remove Duplicates from Sorted Array(Easy)
- 【LeetCode算法练习(C++)】Search in Rotated Sorted Array
- Using CURL to download a remote file from a valid URL in c++