How can I return an array from a function? (God's language++) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How can I return an array from a function? (God's language++)

I've no idea how to return an array from a function in C++, somehow using pointers, but how?

27th Feb 2020, 8:22 AM
Seb TheS
Seb TheS - avatar
17 Answers
+ 6
if you want to use pointers you can use such a code: int* createData(int size){ int* pData = new int[size]; //set values to data... for(int i = 0; i < size; ++size){ //process or set pData[i] } return pData; } void freeData(int *pData) //should be called to free array allocated with createData { delete[] pData; } //use of functions: //.... int *pMyData = createData(100); // work with data freeData(pMyData);
27th Feb 2020, 8:52 AM
andriy kan
andriy kan - avatar
+ 5
The usual way is to return an array in C is through the arguments, not through the return value in order to avoid an alloc and a free call and allows the user to decide which type of array to use. Same way in C++ if you can't use C++ arrays. void f( int* arr, size_t size ) { // fill array } You can return the size as the return value or via argument as well.
27th Feb 2020, 9:04 AM
Dennis
Dennis - avatar
+ 5
If by array you meant instance of std::array class then you can simply return it by referring to its name without caring much about memory leaks. by default a copy of std::array will be returned. auto stl_arr(){ array<int,5> a{5,3,7,9,2}; return a; } use : for(int a : stl_arr()) cout<<a <<" "; If you want to return cstyle(raw) array you'll need to care about memory leaks. If array is declared within function it'll be deallocated after the function returns. the returned pointer will point to something that does not exist or is likely to be deallocated sooner. To solve this: 1.You can declare array outside function , process it inside function and return nothing. or 2.use static qualifier. if you declare array as static it'll have same lifetime as entire program. int * cstyle_arr() { static int a[]{5,3,7,9,2}; return a; } int *a=cstyle_arr(); for(int i=0;i<5;i++) cout<<a[i]<<" "; 3. use dynamic memory allocation. you'll be responsible to deallocate memory you used. delete [] a;
27th Feb 2020, 9:15 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 4
Seb TheS How about struct arr_t{ (const) int* arr; size_t size; }; // or an std::pair, but I don't like the .first and .second syntax. class a{ ... arr_t getArr() (const) { return { arr, size }; } };
27th Feb 2020, 9:22 AM
Dennis
Dennis - avatar
+ 2
Seb TheS, if you put a raw array into a struct, you can return that struct instead. Would that work?
27th Feb 2020, 9:20 AM
HonFu
HonFu - avatar
+ 2
Maybe it would help us give you more fitting advice, if we knew the specific use-case.
27th Feb 2020, 9:30 AM
HonFu
HonFu - avatar
+ 2
Tr0j4n It had a ++ in the end. If: God's language is C, Then: God's language++ is C++
28th Feb 2020, 5:22 AM
Seb TheS
Seb TheS - avatar
+ 1
Are we talking about the type array or the raw C arrays?
27th Feb 2020, 8:42 AM
HonFu
HonFu - avatar
+ 1
HonFu Talking about basic: char[] arr = {'1', '2', '3'}; Arrays, I don't know about the given names.
27th Feb 2020, 8:49 AM
Seb TheS
Seb TheS - avatar
+ 1
char[] arr = {'1', '2', '3'}; if you are talking about how to return an array that local in a function, then you should create of copy of it (using new) or you can copy it to the buffer, that passed as argument to the function. int getArray(char*buffer, int size) { char arr[] = {'1', '2', '3'}; int count = sizeof(arr) / sizeof(arr[0]); if(count > size) count = size; for(int i = 0; i < count; ++i) buffer[i] = arr[i] ; return count; } int main() { const int Size = 10; char myArray[Size]; int count = getArray(myArray, Size); for(int i = 0; i < count; ++i){ cout << myArray[i] << endl; } }
27th Feb 2020, 9:06 AM
andriy kan
andriy kan - avatar
+ 1
Dennis This doesn't help as I need to get a private array of 2 objects, I need a getter method for the arrays.
27th Feb 2020, 9:18 AM
Seb TheS
Seb TheS - avatar
+ 1
HonFu I know nothing about structs, maybe it would work, but using a getter function sounds best way.
27th Feb 2020, 9:23 AM
Seb TheS
Seb TheS - avatar
+ 1
if you store that private array a as pointer, you can just return that pointer with const qualifier, but a user of your getter should also know the size of that array (for example, using other getter that returns size): class A{ int *pData; int size; public: // constructors and other methods... const int* getArray() const { return pData; } int getSize() const { return size; } }; this getter returns pointer to const int array, it will be a user task to create a copy of that array if he/she wants to change it.
27th Feb 2020, 9:24 AM
andriy kan
andriy kan - avatar
+ 1
For exple, you have an array " int Arr[10]" you only write : "return Arr ; " It's the same if your function receive the array like argument or if you create your array in your function
29th Feb 2020, 7:38 AM
Lansana Diakite
Lansana Diakite - avatar
0
Return a pointer
27th Feb 2020, 12:43 PM
Hari kanani
Hari kanani - avatar
0
Another way to do this. In c++ you can copy the array to a vector then passe the vector as function argument, then the function can return the vevtor
27th Feb 2020, 9:37 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
0
Actual Gods language is c
28th Feb 2020, 2:12 AM
Binaryexploit
Binaryexploit - avatar