Best Practices for Operations on Structures
For a program I am working on, I need to perform some operations on vectors, such as vector addition. However, I am unsure of how best to write the functions to perform the operation. My vector is defined as
Code:
struct Vector
{
double x;
double y;
double z;
};
If I want to add two vectors together and return a vector, is there a general best practice for one of these forms?
Code:
Vector * vector_add_1(Vector *v1, Vector *v2)
{
Vector *sum = malloc(sizeof(Vector));
/* Assign values to sum */
return sum;
}
void vector_add_2(Vector *v1, Vector *v2, Vector *dest)
{
dest->x = ...;
/* Snip */
return;
}
Assume the latter function accounts for the case that dest could be v1 and/or v2.