Sorry to open a new question, but I can't find a question like this in the forum or around google..
Anyway, my problem is this: Inside Main I declare an array "insieme_A" and a variable that contains the lenght of the array "nums_element_A"
int main()
{
double *insieme_A;
int nums_element_A;
nums_element_A = get_array(insieme_A);
And then, print the array:
int counter;
printf("\nL'array è costituito dai seguenti elementi: \n");
for (counter = 0; counter < nums_element_A; ++counter)
printf("%lf \n", insieme_A[counter]);`
Then I have a function, exported by a library. In this function I ask the User "how many elements the array must have?
Then create the dynamic array array = (double *)calloc(nums_elements, sizeof (double));
and fill it with elements inside the for loop.
My problem is when the function ends, and in the main I try to print the array.. it prints the number of element inserted by the user.. but all zeros.
If user wants a 5 element array, it prints {0,0,0,0,0}
Instead, if I print the array inside the function, it works without problems. So I am wondering.. is it possible to do it like this, or should I write the array inside a file.. end the function and inside the main open the file and read the array from there?
Thanks a lot
int get_array(double array[])
{
double element;
int nums_elements,
counter;
do
{
printf("Quanti elementi deve contenere l'insieme? ");
scanf("%d", &nums_elements);
}
while (nums_elements <= 0);
array = (double *)calloc(nums_elements, sizeof (double));
for (counter = 0;
counter < nums_elements;
++counter)
{
printf("Inserire valore %d-->", counter+1);
scanf("%lf",
&element);
array[counter] = element;
}
for(counter=0;counter<nums_elements;++counter){
printf("%lf",array[counter]);
}
return (nums_elements);
}