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;
}