Assigning to the real or imaginary part of a complex number in C
I need to work on complex to extract imaginary roots of polynomial using Newton's method.
I'm getting an error, so I broke the code down to simple problem to see what's wrong. When I try to compile it it returns an error:
warning: target of assignment not really an lvalue; this will be a hard error in the future
Also I would like to know if there is anyway I can display the whole complex number without going with creal and cimag .
#include<stdio.h>
#include<complex.h>
int main()
{
double complex z1 = 2 + 3*I;
creal(z1) = 5;
cimag(z1) = 10;
printf("%.2f +%.2f *i n", creal(z1), cimag(z1));
return 0;
}
The problem is these lines:
creal(z1) = 5;
cimag(z1) = 10;
creal and cimag return doubles. You cannot assign to a functions return value. You can assign the return value of a function to another variable like double real = creal(z1) .
上一篇: 在gdb C ++中打印双向量的和
下一篇: 分配给C中复数的实部或虚部
