我想对 X 复数求和,但这段代码返回给我:
-9.3e+61 + -1.9e+062i
typedef struct complex{
double real;
double imag;
} complex;
complex sum(int length, ...)
{
va_list param;
va_start(param, length);
complex out = {0, 0};
for(int i = 0;i<length;i++)
{
out.real += va_arg(param, complex).real;
out.imag += va_arg(param, complex).imag;
}
va_end(param);
return out;
}
当我引用 real / imag 部分时,它会返回正确的 real / imag 结果。
主要的:
int main()
{
complex result;
complex a = {3.1,-2.3};
complex b = {0.5,-3};
complex c = {0,1.2};
result = sum(3,a,b,c);
printf("Sum is %.2g + %.2gi. \n", result.real, result.imag);
return 0;
}
我应该改变什么才能让它工作?