Im trying to create a complex numbers library in C by using a typedef of an structure.
typedef struct{
double real;
double imag;
}complex_t;
However I'm curious whether if it's plausible to use this definition to do some computations using the '+' and '*' sign to operate complex numbers, instead of
creating a complex_t sum(complex_t z,complex_t w)
or complex_t prod(complex_t z,complex_t w)
. I want to do something like this :
#include<complex_library.h>
complex_t z1,z_2,w_1,w_2;
z_1 = newComplex(1,2);
z_2 = newComplex(-3,-4);
w_1 = z_1 + z_2;
w_2 = z_1 * z_2;
Thanks.
I'm almost ashamed to post this as an answer, but it is a valid question and every question deserves an answer.
I'm curious whether if it's plausible to use this definition to do some computations using the '+' and '*'
No, it is not possible to do. C simply does not support operator overloading. C++ supports this, but not C.
Sidenote: Avoid using the _t
suffix. It's reserved in POSIX. Sure, it's not reserved in the C standard but POSIX is pretty big.
Names that end with ‘_t’ are reserved for additional type names.
https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html