×

Return array from function in C

Return array from function in C

C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without the need for an index by determining the name of the array.

If you want to return a single-dimensional array from a function, a function returning a pointer should be declared as in the following example:

int * myFunction() 
 {
    …..
    …..
    …..
 } 

The second thing to remember is that C does not support returning the address of a local variable outside of the function, so you will have to define it as a static variable.

Now, consider the function below that will generate 20 numbers randomly and return them using an array.

#include <stdio.h>
 /* function to generate and return random numbers */
 int * getRandom( ) {
    static int  k[20];
    int p;
    /* set the seed */
    srand( (unsigned)time( NULL ) ); 
    for ( p = 0; p < 20; ++p) {
       k[p] = rand();
       printf( "k[%d] = %d\n", p, k[p]);
    }
    return k;
 }
 /* main function to call above defined function */ 
 int main () {
    /* a pointer to an int */
    int *s;
    int i;
    s = getRandom();
    for ( i = 0; i < 10; i++ ) {
       printf( "*(s + %d) : %d\n", i, *(s + i)); 
    }
    return 0;
 } 

Output:

Return array from function in C

Using Dynamically Allocated Array

Dynamically allocated memory (allocated using new or malloc ()) remains their memory until delete or free) (is removed. So we can build a dynamically allocated array and will be able to delete it once we get out of use.

#include <stdio.h> 
 int* function(int *array) 
 { 
     arr[1] = 20; 
     arr[2] = 40; 
     return array; 
 }  
 int main() 
 { 
     int arr[100]; 
     int* ptr = function(arr); 
     printf("%d %d", ptr[1], ptr[2]); 
     return 0; 
 } 

Output:

Return array from function in C

Using struct:

We can wrap a structure/class list and return a struct/class instance. The explanation for this work is that there are deep copies of array members of structures. A deep copy occurs when the returned instance is copied to main in the program below.

#include <stdio.h> 
 struct arrayWrap { 
     int array[100]; 
 }; 
 struct arrayWrap function() 
 {  
     struct arrayWrap m; 
     m.array[1] = 25; 
     m.array[2] = 49; 
     return m; 
 } 
 int main() 
 { 
     struct arrayWrap m = function();  
     printf("%d %d", m.array[1], m.array[2]); 
     return 0; 
 } 

Output:

Return array from function in C

Using static array:

A static variable's lifetime is throughout the program. So, we can always build and return a static local array.

#include <stdio.h> 
 int* function() 
 { 
     static int array[200]; 
     /* Some operations on array[] */
     array[4] = 50; 
     array[9] = 80;  
     return array; 
 } 
 int main() 
 { 
     int* ptr = function(); 
     printf("%d %d", ptr[4], ptr[9]); 
     return 0; 
 } 

Output:

Return array from function in C

Related Topics

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Different ways to Declare the Variable as Constant in C

There are a wide range of ways of making the variable as steady Using const keyword: The const catchphrase permits a software engineer to inform the compiler that a specific variable should...

5 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.