c - 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 & lt; n; i ++) results [i] = one [i] -b [i]; Return result; }
and then in the main:
double * vec = vec_substraction (a, b, n); For (i = 1; i & lt; n; i ++) printf ("% d", vec [i]);
A and B elements have the same number of vectors, n is the number of elements.
Yes you can, but you can send anywhere results
to memory Need to allocate.
Actually, you can either make it static or dynamic if you allocate it out, either outside of vec_subtraction
or vec_subtraction
.
If you are going to allocate in:
double * vec_subtraction (char * a, char * b, int n) {double * result = malloc (sizeof ( Double) * n); Int i; For (i = 0; i & lt; n; i ++) results [i] = one [i] -b [i]; Return result; }
and in the main:
double * vec; // ... vec = vec_subtraction (a, b, n); // ... Free (VCC); Do not forget to call
after some time.free
after the callvec_subtraction
If you are going to allocate out then you have to pass the pointer to memory:
zero vec_subtraction (four * one, four * b, int N, double * result) {int i; For (i = 0; i & lt; n; i ++) results [i] = one [i] -b [i]; }
In the main:
// Select one of these: // double * vec = malloc (sizeof (double) * n) ; // Double Wake [10]; // where 10 = n Vec_subtraction (a, b, n, vec); // If you use * vec = malloc ... call free (vec)
Comments
Post a Comment