I am using Code-Blocks with the mingw GCC compiler for windows to code my program in C. I have a function defined where the input is an array of type int. I have another struct defined as
typedef struct {
float re;
float im;
}complex_float;
I want to convert the int type array to a complex_float type array as I need to process the data in the complex_float format. I am using the following pointer method to do the conversion
complex_float *comSig = (complex_float *) sigbuf;
where sigbif is an int pointer pointing to the start address of the integer array.
But when i do a printf("%f",comSig[0].re); I am getting some garbage values like -1.#QNAN0.
I have used this technique for data conversion between arrays a number of times on LINUX and it works. Is this a problem related to the mingw compiler not working clearly or is it related to the fact that I am using an incorrect method for converting an int array to struct array.
This is absolutely wrong what you’re doing. Casting a value’s type doesn’t “convert” the values but just tells the compiler to treat it as a different type. As an int and your struct most probably have different sizes, so do the arrays created from them, so you even write/read past the bounds of your integer array. If you really want to convert the integers to complex numbers, you have to code it yourself:
Of course, don’t forget to
free(cpx_arr)after use.