简体   繁体   中英

How to return an array of pointers?

I have just made this code to calculate variance and mean of an array of numbers, but I can't find a way to return an array of pointers.

This is my function:

float* statistics(int numbers[], int numbers_len, float * ptr_mean,
float * ptr_variance, float * ptr_stdev, int * ptr_median){

  float*mean; float*variance; float stat[4]; float*array_stat;
  int i=0;

  mean=calculate_mean(numbers, numbers_len);
  variance=calculate_variance(numbers, numbers_len, mean);
  stat[0]=*mean;
  free(mean);
  mean=NULL;

  stat[1]=*variance;
  free(variance);
  variance=NULL;

  for(i=0;i<2;i++){
    array_stat=(float*)calloc(2, sizeof(float));
    array_stat[i]=stat[i];
  }

  return array_stat;
}

I need array_stat to be an array of pointers. In int main() I call the function *statistics() this way:

int main(){
  int array_numbers[MAX_ITEMS];
  int n_numbers, i=0;
  float *calculate_statistics;

  calculate_statistics = statistics(array_numbers, n_numbers, ptr_mean,  
  ptr_variance, ptr_stdev, ptr_median);
  for(i=0;i<2;i++){
     printf("\n[%d] ITEM > %0.2f\n", i+1, *calculate_statistics);
  }
  free(calculate_statistics);
  calculate_statistics=NULL;
}

Someone can help me please?

In statistics()

array_stat=(float*)calloc(2, sizeof(float));
  for(i=0;i<2;i++){
    array_stat[i]=stat[i];
  }

In main()

for(i=0;i<2;i++){
     printf("\n[%d] ITEM > %0.2f\n", i+1, calculate_statistics[i]);
  }

Do you only ever want an array of two floats? You might be better off using a struct.

In C and such languages, "arrays" (not spécifically arrays of pointers) are half-baked data types which prevents to return them easily as a single object as you would like.

You can't have them as results returned by a fonction

 int[4]  foo(..parameters...) // no
 {
      int tmp[4];
      ....
      return tmp;             // no
 }

A common way to do it it to make the fonction allocate some space, and return a pointer

int*    foo( ...parameters...) 
{
     int *tmp = malloc(4 *sizeof(int));
     ....
     return tmp;
}

an thus the caller will have to deal with a pointer. Looks mostly like an array (you can use the [] notation for indices), but beware of memory leaks: you'll have to manage the deallocation.

Another approach is to call the function with an extra parameter: the address where the function will put the result.

 void  foo(...parameters...,  int results[4])
 {
      // do some stuff
      // put the results in, hum, results
 }

Calling sequence:

 int results[4];
 ...
 foo(12, 345, 6789, results);
 // here we are

So you won't have to manage allocation/deallocation, because the array is an automatic variable (lives on the stack) and, moreover, you won't have to copy data from array to array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM