Can I return double * in function?

13,157

Solution 1

Yes you can, but you need to allocate memory for result somewhere.

Basically, you can either allocate the memory inside vec_subtraction or outside vec_subtraction, if you allocate outside you can do this statically or dynamically.

If you're going to allocate inside:

double *vec_subtraction (char *a, char *b, int n) {
    double *result = malloc(sizeof(double)*n);
    int i;
    for(i=0; i<n; i++)
        result[i] = a[i]-b[i];

    return result;
}

and in main:

double *vec;
// ...
vec = vec_subtraction(a, b, n);
// ...
free(vec); 

Don't forget to free the result of the call to vec_subtraction sometime later.


If you're going to allocate outside you need to pass in a pointer to the memory:

void vec_subtraction (char *a, char *b, int n, double *result) {
    int i;
    for(i=0; i<n; i++)
        result[i] = a[i]-b[i];
}

in main:

// choose one of:
// double *vec = malloc(sizeof(double)*n);
// double vec[10]; // where 10= n.
vec_subtraction(a, b, n, vec);

// if you used *vec = malloc... remember to call free(vec).

Solution 2

Not like that. You need to allocate memory either on the stack before you call the function or on the heap from within the function.

double *vec_subtraction( ... ) {
     double *result = malloc(sizeof(double)*n);

     ...

     return result;
}

Then the rest would work. You need to remember to free the memory though.

The other option is:

void vec_subtraction( ..., double *result ) {


         ...

}

Then in main:

 double result[n];
 vec_subtraction(..., result);

Solution 3

You can certainly return a double* from a function. Just make sure the pointer still points to a valid object. e.g. do not do this:

double *vec_subtraction (char *a, char *b, int n) {
double result[n];
int i;

for(i=0; i<n; i++)
    result[i] = a[i]-b[i];

    return &result[0]; //error,the local array will be gone when the function returns.
}

This would be fine though:

double *vec_subtraction (char *a, char *b, int n) {
double *result = malloc(sizeof(double)*n);
int i;
if(result == NULL)
   return NULL;

for(i=0; i<n; i++)
    result[i] = a[i]-b[i];

    return result; //remember to free() the returned pointer when done.
}

Solution 4

You can, but you don't seem to be allocating any memory for the result vector.

Solution 5

In your vec_subtraction function you have an uninitialised variable in double *result. If you want this to return something meaningful you need to allocate some memory to the array, e.g.

result = malloc(sizeof(double) * n);

Then you have to remember to free it when you're done:

free(vec);

However, good practice (at least when I was a C-coder) was to alloc memory and free memory in the same scope, where possible of course. So really you should pass your array in to your vec_subtraction function. You'll need to change your function's signature to:

vec_subtraction (char *a, char *b, int n, double *result)

calling it like this:

double vec[n];
...
vec_subtraction (a, b, n, &result);

Excuse my pseudo-c, but hopefully you get the idea.

Good luck!

Share:
13,157
Devel
Author by

Devel

Updated on June 04, 2022

Comments

  • Devel
    Devel almost 2 years

    Can I do something like this? Will this work?

    double *vec_subtraction (char *a, char *b, int n)
    {   
        double *result;
        int i;
    
        for(i=0; i<n; i++)
            result[i] = a[i]-b[i];
    
        return result;
    }
    

    and then in main:

    double *vec=vec_substraction(a, b, n);
    for(i=1; i<n; i++)         
        printf("%d", vec[i]);
    

    a and b are vectors with the same number of elements, n is number of elements.