Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I return an entire array from a different function to the main function in C++

+5 votes
asked Oct 30, 2025 by BoneGamer The cyndaquill (170 points)

I'm trying to figure out how to return an array created within a function to the main function. Ideally another array created within the main function can receive the exact array returned from the other function.

For example something like this:

string createStringArray(int);
double createDoubleArray(int);

int main()
{
    int size = 5;

    string array1[size];
    double array2[size];

    array1[size] = createStringArray(size);
    array2[size] = createDoubleArray(size);
}

string createStringArray(int size)
{
    string array[size];

    //code that inputs values into the array

    return array;
}

double createDoubleArray(int size)
{
    double array[size];

    //code that inputs values into the array

    return array;
}

Obviously this is a very simplified version but I just DON'T KNOW HOW TO MAKE IT RETURN THE ARRAY. Like what I want is for an array to be created within the function and then returned to the main function, and an array created within the main function becomes an exact copy (other than the name of the array) of the array created within the other function.

2 Answers

0 votes
answered Nov 1, 2025 by Durga Namboothiri (180 points)

Hello jave  computer class 


ABblush

0 votes
answered Nov 2, 2025 by Peter Minarik (101,340 points)

I haven't worked with C++ recently, but I made a sample code for you below.

The main idea is that arrays are used when you know the size and/or the elements in it. If you do not know what goes in there, you'll use a pointer to an array and you keep the size in a different variable (see array2 and numberOfElementsInArray2).

CreateDoubleArray creates (allocates) a new array for a specific size, but it also means you'll need to release the memory, so you'd need to call delete[].

GetData receives a pointer to the memory, where the array is located, and the maximum number of elements that array could hold (meaning the size the caller allocated for the array) to prevent buffer overrun.

But this is mostly C. The C++ way is to use std::vector for your arrays.

#include <iostream>
#include <vector>

double * CreateDoubleArray(size_t size)
{
    double * array = new double[size];
    for (size_t i = 0; i < size; i++)
        array[i] = (double)i;
        
    return array;
}

size_t GetData(double * array, size_t maxSize)
{
    const size_t elementCount = 3;
    if (maxSize > elementCount)
        "Array size is too small.";
    
    for (size_t i = 0; i < elementCount; i++)
        array[i] = i;

    return elementCount;
}

void PrintArray(double * array, size_t size)
{
    for (size_t i = 0; i < size; i++)
        std::cout << array[i] << " ";
    
    std::cout << std::endl;
}

std::vector<double> GetData(size_t size)
{
    std::vector<double> data;
    for (size_t i = 0; i < size; i++)
        data.push_back((double)i);
    
    return data;
}

int main()
{
    size_t size = 5;
    double * array1 = CreateDoubleArray(size);
    // Do something with array1, we'll just print it here.
    PrintArray(array1, size);
    
    double array2[size];
    size_t numberOfElementsInArray2 = GetData(array2, size);
    // Do something with array2, we'll just print it here.
    PrintArray(array2, numberOfElementsInArray2);
    
    delete[] array1; // Release the allocated memory of array1
    
    // In C++, use vector instead of arrays
    std::vector<double> vector = GetData(size);
    for (const double & element : vector)
        std::cout << element << " ";
    
    std::cout << std::endl;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...