views:

286

answers:

7

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.

+10  A: 

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).
Mark E
Also `double vec[n];` in C99 and later.
Mike DeSimone
+2  A: 

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

Mick Sharpe
A: 

It would work if you allocated any memory for the results:

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

And now it becomes the responsibility of the caller to free that memory.

The caller might also pass the double array to be filled with results, in which case you might choose not to use a dynamically allocated array.

UncleBens
+3  A: 

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);
Neal
+2  A: 

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.
}
nos
+1  A: 

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!

brindy
+1  A: 

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.

Jerry Coffin