I am relative new to programming so I apologize if my question is basic.
Situation:
I have several float values and an array of pointers to each value. Ex:
float nr1=1.15;
float nr2=2.30;
float nr3=23.34;
....
float * my_address_array[3];
my_address_array[0] = &nr1;
my_address_array[1] = &nr2;
my_address_array[2] = &nr3;
To access one element, I can use:
float temp_value;
float ** ptr_value;
...
ptr_value = &my_address_array[0];
temp_value = **( ptr_value+0); // copy nr1 to temp
temp_value = **( ptr_value+1); // copy nr2 to temp
temp_value = **( ptr_value+2); // copy nr3 to temp
So far so good. On my system float occupies 32 bits (8051 microcontroller). I need to take one float number and separate it into four 8 bit variables. Example for nr2:
My attempt was:
unsigned char storage1;
unsigned char storage2;
unsigned char storage3;
unsigned char storage4;
...
storage1 =(unsigned char) ((**( ptr_value+1)) >> 24) ;
storage2 =(unsigned char) ((**( ptr_value+1)) >> 16) ;
storage3 =(unsigned char) ((**( ptr_value+1)) >> 8) ;
storage4 =(unsigned char) ((**( ptr_value+1)) & 0xff) ;
I get bad operand type. It seems that I cannot use bit shift operations with float numbers (at least google sais that). I can add a new pointer, as in:
char ** ptr_char_value;
...
ptr_char_value = &my_address_array[0]; // generates warning
storage1 = (*(*( ptr_char_value+1)+0));
storage2 = (*(*( ptr_char_value+1)+1));
storage3 = (*(*( ptr_char_value+1)+2));
storage4 = (*(*( ptr_char_value+1)+3));
I do get a warning (which is fair) that I am using a char type pointer for a float value. I am also not sure how reliable this is. Can anyone advise a better solution?
Thank you!
Edit: The code is for a 8051 microcontroller. I would like to make it as fast/optimal as possible.
try this. You should type cast before you can shift the data. Include stdint.h