How to return a local array from a C/C++ function?

Consider the below C++ program. Is it right way of returning array from a function?

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int* fun()
{
    int arr[100];
  
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int* ptr = fun();
    printf("%d %d", ptr[0], ptr[1]);
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int* fun()
{
    int arr[100];
  
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int* ptr = fun();
    cout << ptr[0] << " " << ptr[1];
    return 0;
}

chevron_right



Warning:

In function 'int* fun()':
6:8: warning: address of local variable 'arr' returned [-Wreturn-local-addr]
    int arr[100];
        ^
Output:

10 20

The above program is WRONG. It may produce values 10 20 as output or may produce garbage values or may crash. The problem is, we return address of a local variable which is not advised as local variables may not exist in memory after function call is over.

So in simple words, Functions can’t return arrays in C. However, inorder to return the array in C by a function, one of the below alternatives can be used.

Following are some correct ways of returning array:

Using Dynamically Allocated Array :

Dynamically allocated memory (allocated using new or malloc()) remains their until we delete it using delete or free(). So we can create a dynamically allocated array and we can delete it once we come out of function.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int* fun(int *arr)
{
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int arr[100];
    int* ptr = fun(arr);
    printf("%d %d", ptr[0], ptr[1]);
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int* fun()
{
    int* arr = new int[100];
  
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int* ptr = fun();
    cout << ptr[0] << " " << ptr[1];
    return 0;
}

chevron_right


Output:

10 20

Using static array:

Lifetime of a static variable is throughout the program. So we can always create a local static array and return it.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int* fun()
{
    static int arr[100];
  
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int* ptr = fun();
    printf("%d %d", ptr[0], ptr[1]);
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int* fun()
{
    static int arr[100];
  
    /* Some operations on arr[] */
    arr[0] = 10;
    arr[1] = 20;
  
    return arr;
}
  
int main()
{
    int* ptr = fun();
    cout << ptr[0] << " " << ptr[1];
    return 0;
}

chevron_right


Output:

10 20

Using struct:

We can wrap array in a structure/class and return an instance of the struct/class. The reason for this work is, array members of structures are deeply copied. In below program deep copy happens when we returned instance is copied in main.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
struct arrWrap {
    int arr[100];
};
  
struct arrWrap fun()
{
    struct arrWrap x;
  
    x.arr[0] = 10;
    x.arr[1] = 20;
  
    return x;
}
  
int main()
{
    struct arrWrap x = fun();
    printf("%d %d", x.arr[0], x.arr[1]);
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
struct arrWrap {
    int arr[100];
};
  
struct arrWrap fun()
{
    struct arrWrap x;
  
    x.arr[0] = 10;
    x.arr[1] = 20;
  
    return x;
}
  
int main()
{
    struct arrWrap x = fun();
    cout << x.arr[0] << " " << x.arr[1];
    return 0;
}

chevron_right


Output:

10 20

This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : RishabhPrabhu



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.