2

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);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
lordcroci
  • 83
  • 8
  • you are sending into your get function a copy of the array val- so you are not really modifying the place it is pointing to out side in main- you should try and send get_array(double *array[]) – H.cohen Dec 17 '18 at 11:14

1 Answers1

3

This is because in your code, the parameter of get_array function is passed by value.

To correct it use it as int get_array(double **array) and make change in your code accordingly. Following is code snippet. See complete working code here:

int get_array(double **array)
{
    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", &((*array)[counter]));
    }
    return (nums_elements);
}

To call, do following:

int count;
double *insieme_A;
count = get_array(&insieme_A);
cse
  • 4,066
  • 2
  • 20
  • 37