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

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.