Arrays are specific containers that have values of the same data type. Functions in C perform operations on arrays, and these arrays are then returned to the main function. There are many approaches to describe this phenomenon. In this guide, some common methods are explained:

Use Pointers to Return the Static Array

When we use a normal array, there are chances of having some sort of abnormal results. To avoid this, we use a static array in our C code. Let us understand the example we have used. In this function, we have declared an array with 5 values the return type as mentioned here.

Int *function ()

As the value will be an integer type, so it is tagged as “int” in example below. As we have introduced the function as a pointer, the function will be a pointer type. After entering the values, an array is returned to the main program.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-1.png" data-lazy- height="473" src="data:image/svg xml,” width=”693″>

In the main program, we have made a function call. To accept the value that is returned from the function, we will use an integer variable. When the array is returned, we can access its values easily. The values will be printed manually.

Int* pointer = function ();

The purpose of the pointer is to locate the item that is present on index one of the array. In other words, it shows the address of the value in the array. Then, we use a function prototype that will return the pointer.

To see the output of the array returned through the function, we need to have access to the Ubuntu terminal in the case of Linux. This is due to the output being accessible through the Linux terminal. In Linux, we need a compiler to run C codes written in any text editor. This compilation is done through G . The “-o” is used to store the output in a file. Here, we need the output file and the source code file. After compilation, we will execute the code:

$ g -o file1 file1.c


$ . /file1

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-2.png" data-lazy- height="59" src="data:image/svg xml,” width=”453″>

From the output, we can see the array, which was initialized in the function, is displayed in the main function by using a static array, initialized manually and through the pointers.

Return Dynamically Allocated Array Using Pointers

Arrays can be returned by using dynamic allocation. Arrays can be dynamically allocated by using the word “new”. They will remain there until we delete them by ourselves. Static arrays are fixed in size, which means you have to provide size during initialization. Once the array is created, then it is hard to increase the size at run time or hereafter. But in the case of the dynamic array, we can add more items whenever we want because it expands as we enter values in it. So we don’t need to specify or identify any size.

Moving towards the example we have used here. We have used a dynamic array with the pointers as in the previous examples, where we have used pointers with the static arrays.

After function declaration, arrays are declared dynamically:

Int *array = new int [100];

The term, “new”, is constantly used to create a dynamic array. We will perform operations on the array by entering values in it. After that, the array is returned to the main program:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-3.png" data-lazy- height="490" src="data:image/svg xml,” width=”681″>

Now, consider the main function. We have made the function call. As the array is returned, we add a pointer integer type variable to accept the value.

Int* pointer = function ();

The values that were stored in the array are printed manually. The output is obtained through the compilation and execution method.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-4.png" data-lazy- height="59" src="data:image/svg xml,” width=”429″>

Return Array Using the Structures

Structures are the containers like arrays. But array contains the value of the same data type at a time. And in the case of structures, they contain more than one data type value. We have taken a structure named “sample”. Here, the array declaration is inside the structures instead of functions. The return type is the name of the structure. The structure variable is returned to the main program. The structure uses the word “struct” for declaration.

Struct sample

{


Int arr[100];

};

After the structure declaration, we have used a function in which an object of structure is created. This object will be used to access the structure. This function will return the object of structure to the main function so that we can print the array through this object. A variable will get the values in the variable. This value is the integer number up to which we will enter values in the array. As in this example, we have selected 6 as the number. So, the numbers will be entered up to 6 in the array.

Struct sample func (int n)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-5.png" data-lazy- height="404" src="data:image/svg xml,” width=”669″>

Now, moving towards the main program, we have created an object to access the array through this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-6.png" data-lazy- height="283" src="data:image/svg xml,” width=”409″>

After object initialization, a value is added to the variable up to which we want the numbers to be entered in the array. In a function call, we will pass the value in the parameter:

We will have the display by using the for loop. The values are displayed through the object declared at the start of the main program:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-7.png" data-lazy- height="150" src="data:image/svg xml,” width=”446″>

The output indicates that 6 values are shown in the result as we have entered 6 numbers in the program.

Return Array Using Std

C uses many methods to return an array from the function. One of them is through std::array. It is a template of structure. This feature also provides two more functions that are size () and empty (). An array name is returned that indicates that the whole array is returned to the main program. Here, we will add a header file “array”. In addition to the library, it contains all the functions of the array.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-8.png" data-lazy- height="495" src="data:image/svg xml,” width=”667″>

Since we can return the whole array with the name of it, so in the declaration of a function, we will use the array as a return type. Data is entered in the array. After that, the array will be returned to the main program. Moving towards the main function, an array variable will accept the array when the function is called.

Again, for loop will be used for displaying array values. We observe the output from the image displayed below. As we have used 10 sizes, 0 numbers will be entered. Hence, these are displayed:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-9.png" data-lazy- height="222" src="data:image/svg xml,” width=”421″>

Return Array Through Vector Container

This approach is a dynamically allocated array. As in this case, there is no need to specify array size. We don’t need any size parameter here. Using this example, we need to add a “vector” header in the library that contains the functionalities of the vector.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-10.png" data-lazy- height="499" src="data:image/svg xml,” width=”659″>

Moving toward the function, where the return type is also an int vector and also contain a vector pointer as an argument in the parameter. An array with the name “temp” is introduced here:

Vector <int> MultiplyArrayByTwo(const vector<int> *arr)

The function will multiply the elements of the array by two through using the tmp.push_back () function. Then, return the tmp. An auto-type variable will accept the values of the array from the function. The array contains the items in it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Return-Array-From-Function-C-11.png" data-lazy- height="221" src="data:image/svg xml,” width=”432″>

The output shows the working of the vector container.

Conclusion

In the aforementioned article, we have described the five most commonly used methods to explain the functionality of returning an array from the function.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Author-Image-150×150.jpg6100f07cb43f5.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.