-9

I can convert it if the doulble * is pointing only to one double but not an array.

Here is my function,

double fun(double * in){
  return *in;
}

My double * in has 9 elements and I'm trying to pass them to an array out[9];

SamuelNLP
  • 4,038
  • 9
  • 59
  • 102

2 Answers2

-1

You cannot return an array, you need to declare it outside the function:

void fun(double *out, double *in, size_t num_items){
  int i;
  for (i = 0; i < num_items; i++) {
    out[i] = in[i];
  }
}

// Somewhere else in your code:
double destination[9];
fun(destination, in, 9); // Copy 9 doubles into destination
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
-1
#include <stdio.h>
#include <string.h>

typedef struct dbl9 {
    double array[9];
} Dbl9;

Dbl9 fun(double *in){
    Dbl9 ret;
    memcpy(ret.array, in, sizeof(ret.array));//There can be no assurance that <in> the correct
    return ret;
}

int main(){
    double array[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
    Dbl9 dbl9= fun(array);
    int i;
    for(i=0;i<9;++i){
        printf("%f\n", dbl9.array[i]);
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Of course, that returns a structure through the stack is not efficient but It is the closest to the image that returns an array. – BLUEPIXY May 21 '14 at 16:31