How to return array from function in C++?

How to return array from function in C++

In this article, we will see how to return array from function in C++.

It is not possible to directly return an array to a function call and it can be done by using pointers. If you declare a function with a pointer return type that returns the address of the C-type array, then it will not work all the time in C++. You will get a warning message from the compiler and it can also show some abnormal behaviour in the output.
In this article, we have mentioned five methods to return an array from a function in C++.

Using a static array with pointers

We can handle the abnormal behaviour of the normal array by declaring it as static. It is the only way to avoid warnings and unwanted results.
Code:

Output:

101 102 103 104

Using dynamically allocated array

Arrays can be dynamically allocated using malloc() or new and they will remain there until we delete them. We will delete the array after coming out of the function.

Code:

Output:

101 102 103 104

Using structs

We can declare an array inside a structure in C++. We can return an instance of the struct because the elements in the array of structures are copied deeply.

Code:

Output:

Elements present in the array are : 0 1 2 3 4 5 6 7 8 9

Using std:: array

C++ also offers another choice for returning an array from a function – std:: array. It is a structure template that wraps a regular array and it also provides extra methods like size() & empty(). We can return the array name to return the whole array at the time of the function call. We also need to include the header file ‘array’ to use this method.

Code:

Output:

Elements in the array are: 0 1 2 3 4 5 6 7 8 9

Using vector container

We can store our array elements in a vector container and its size can increase or decrease dynamically. There is no need for a size parameter in this case.

Code:

Output:

Array = [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ]

Conclusion

In this article, we discussed five different methods to return array from function in C++. Any method can be used as per the requirements.

Happy Learning!!

Related Posts

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusion This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays and constructors in […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++Conclusion The files are used to store the data permanently on a disk. This mitigates the problem of […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusion An array is a […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusion This article discusses the vector and how to print vector in C++. Vectors are […]

  • Print Map in C++
    15 March

    Print Map in C++

    Table of ContentsMap vs Unordered MapPrint Map in C++ Using IteratorPrint Map in C++ Using Range-Based for LoopPrint Map in C++ by Overloading the ostream Insertion Operator (<<)Print Map in C++17 Using Structure Binding and Range-Based for LoopPrint Map in C++17 Using for_each() FunctionConclusionReferences Maps in C++ store the data in form of key and […]

  • How to write binary files in C++
    14 March

    Write Binary Files in C++

    Table of ContentsOpening a File in Binary Mode Using fstream LibraryWriting the Binary File Using the write() Function of fstream LibraryWriting the Binary File Using the put() Function of fstream LibraryWriting the Binary File Using Insertion Operator (<<) Of fstream LibraryWriting the Structure Object to Binary File Using fstream LibraryConclusion In this post, we will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.