Is there any way to declare an array inside main function but initialization of this array will be into another function (out of main function)?. If possible, after initialization, is there any possibility to use those initialized data inside main function. I made the following program. However I was unable to run.
Code:
#include <stdio.h>
#include <stdlib.h>
struct sample
{
public:
void func(double *arrD)
{
int length = 3;
double testData[3] = { 3, 7, 6 };
arrD = (double*)malloc(length * sizeof(double));
for (int i = 0; i < length; i++)
arrD[i] = testData[i];
}
};
int main()
{
struct sample sam1;
double* arr = nullptr;
sam1.func(arr);
for (int i = 0; i < 3; i++)
printf(" %d \n", arr[i]);
return 0;
}