Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I return double * in function?

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.

like image 268
Devel Avatar asked Jan 22 '10 21:01

Devel


People also ask

Can a function be double?

The DOUBLE function returns a floating-point number corresponding to a: number if the argument is a numeric expression. character string representation of a number if the argument is a string expression.

Can a function in C be a double?

Every C function must specify the type of data that is being generated. For example, the max function above returns a value of type "double". Inside the function, the line "return X;" must be found, where X is a value or variable containing a value of the given type.

Can return type of main function be double?

Yes. According to C standard, main() should return a int value.


6 Answers

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).
like image 193
Mark Elliot Avatar answered Oct 13 '22 06:10

Mark Elliot


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);
like image 30
Neal Avatar answered Oct 13 '22 05:10

Neal


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

like image 35
Mick Avatar answered Oct 13 '22 05:10

Mick


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.
}
like image 39
nos Avatar answered Oct 13 '22 06:10

nos


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!

like image 34
brindy Avatar answered Oct 13 '22 06:10

brindy


You can do something like this, as long as it's not too much like it. Specifically, you need to assure that you're handling the lifetimes of all the memory reasonably.

Quite a few others have pointed out that you need to allocate space for your result, so I won't get into that part. There are a few other points to think about though.

Right now, you've defined a and b as pointers to char, but you're having your function return a pointer to double. That's harmless but probably pointless and wasteful. What's much worse is that you're then passing those doubles to printf with the "%d" conversion, so it's trying to treat the as integers instead of doubles (you'd print a double with "%f"). The result in this case is undefined behavior.

like image 31
Jerry Coffin Avatar answered Oct 13 '22 05:10

Jerry Coffin