C Return Array
Overview
An array is a collection of different kinds of variables with a similar type of data where the data is stored at an adjacent memory location and to access the data, indexing is used that starts from 0.
For example - Different types of cars like Mercedes, BMW, and Volvo are different variables but the same category that is cars can be called an array.
In C, a return array can be achieved using a struct, static array as well as a dynamically allocated array. To learn all about the c return array, read the complete article.
What is an Array?
You want to buy a phone but before that, you did some research on phones and made a list of all the phones that you liked. So, the list of phones is known as an Array.
Array is a collection of items that are of the same data types at adjacent memory locations.
We can locate the position of the element by adding an offset(i.e., the difference between the two indexes) and the base value(i.e., the indexing of the first element that is 0).
Let us now understand the c return array where we will first pass an array to a function as well as pointers and return an array.
How to Pass an Array to a Function?
We cannot pass a complete array as an argument to a function therefore we can specify the name of the array rather than specifying the index of the elements of an array and pass that array using pointers.
Suppose, you want to pass a single-dimensional array so to do that we have three different ways - pass an array as a pointer, sized array, or an unsized array.
You can refer to pass an array to a function to understand passing an array to a function in C programming in depth.
Passing Array to a Function as a Pointer
If you want to return a 1-D array(single dimension array) then a function returning a pointer should be declared.
In C programming language, the address of the pointer is generally not returned outside the function therefore, we will have to define the local variable as a static variable otherwise the program will give a warning. Let us understand it with an example.
In the following example, we declare a function named numbers and pass an array named num, and examine will this program in c returns an array.
Output
Exception
If we do not add a static variable then the warning is given that the function returns the address of the local variable.
Returning an Array from a Function
Ways of Returning an Array to a Function
The following are some ways of returning an array to a function in the C programming language.
Using Dynamically Allocated Array
Create an array dynamically inside a function and return the pointer to the base array.
Malloc() or new() can be used as dynamic memory allocation. Once we come out of the function we delete the dynamically allocated array using the delete or free().
Output
Using Static Array
We will use a static variable so that it can be used in the whole program. In the program below, arr[] is declared as a static variable that is accessible in the whole program. Therefore the actual memory location of the variable defined as a static variable inside the function Array will be returned.
Output
Using Structure
Using struct different types of data types can be stored. Let us take an example of a student where we want the name and the roll number of the students. Here, we will create a program that will return an array. Each element of an array will represent the structure object.
Output
Related Links
You can refer to the below mentioned links to get more information on arrays: -
Conclusion
- An array is a collection of data of a similar type.
- A c return array can be achieved using structure, dynamic memory allocation i.e., malloc(), and using a static array.
- For dynamic memory allocation we have malloc(), calloc(), delete and free().
- In structure, different data types can be added to an array.
- By declaring the static variable, the variable can be accessed throughout the program.
- We decalre the local variable as static variable to return the address of the pointer outside the function.