Reputation: 11
The below code is throwing error in Visual Studio C project. That same code is working in Linux with GCC compiler. Please let me know any solution to execute properly in Windows.
#include<stdio.h>
#include <complex.h>
#include <math.h>
typedef struct {
float r, i;
} complex_;
double c_abs(complex_* z)
{
return (cabs(z->r + I * z->i));
}
int main()
{
complex_ number1 = { 3.0, 4.0 };
double d = c_abs(&number1);
printf("The absolute value of %f + %fi is %f\n", number1.r, number1.i, d);
return 0;
}
The error I am getting was
C2088: '*': illegal for struct
Here what t I observed the I macro not working properly... So is there any other way we can handle in Windows?
Upvotes: 1
Views: 986
Reputation: 17678
error C2088: '*': illegal for struct
This is the error MSVC returns when compiling the code (as C) for this line.
return (cabs(z->r + I * z->i));
The MSVC C compiler does not have a native complex
type, and (quoting the docs) "therefore the Microsoft implementation uses structure types to represent complex numbers".
The imaginary unit I
a.k.a. _Complex_I
is defined as an _Fcomplex
structure in <complex.h>
, which explains compile error C2088, since there is no operator *
to multiply that structure with a float
value.
Even if the multiplication worked out by some magic, the end result would be a float
value being passed into the cabs
call, but MSVC declares cabs
as double cabs(_Dcomplex z);
and there is no automatic conversion from float
to _Dcomplex
so the call would still fail to compile.
What would work with MSVC, however, is replace that line with the following, which constructs a _Dcomplex
on the fly from the float
real and imaginary parts.
return cabs(_Dcomplex{z->r, z->i});
Upvotes: 1