I'm currently learning C, and I was wondering if there was a really elegant way of a struct variable being able to self assign to it's member variables. i.e.
typedef struct {
double x; double y; double magn = sqrt(pow(x,2) + pow(y, 2));
} vector2d_t
Clearly this does not work. Is it possible to make some type of pre-proc macro, or wrap the structure within something else so the magnitude is automatically assigned every time the members x, y are changed?
Is there some sort of agreed upon method for doing this, or is it necessary to create a function:
void magnitude(vector2d_t *A){A->magn = sqrt(pow(A->x, 2) + pow(A->y, 2));}
and call it every time you create a new vector2d_t?