I have an array of floating point values which represents an a series of complex numbers (so the first value of the array, say x[0], is the real part of the first complex number, x[1] is the imaginary part of the first complex number, x[2] is the real part of the second, and so on...).
My problem is that I want to be able to access these numbers as if they were in a structure format, i.e.
struct cmpx
{
float real;
float imag;
};
typedef struct cmpx COMPLEX;
So I create a union like so:
typedef union complexdata
{
float numbers[2];
COMPLEX cart; //Stands for cartesian
}complexpair;
So my simplified main() function looks like this:
void main(void)
{
float x[10]={1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0};// In reality this is much longer
complexpair *x_ptr;
x_ptr->numbers[0] = x;
}
This is clearly wrong and I get the error:
a value of type "float *" cannot be assigned to an entity of type "float" for the line "x_ptr->number[0] = x;"
Is it possible to use a pointer of union type the way I've defined above to point to an array of floating point values? So that I can access the data in the structure/cartesian format as well treating it as an array of two floating point values?
Thank you for your help.
UPDATE: Just to make it clear why I want to do this;
I have an FFT function with the prototype:
void fft(COMPLEX *Y, int M, COMPLEX *w)
So would like to pass x as the first argument, I have variable w which is exactly the same format as x. When I try something like:
fft((COMPLEX)x->cart, N, (COMPLEX)w_ptr->cart);
this throws an error. I hope the motivation behind this is clearer? Many thanks.