Hi,
I have a Long Double (ld) variable which is 8-bytes long, and i need to write on eeprom to save settings for initializing of the system. As we know eeprom works on 1-byte memory sections so i have to break down the 8-btye ld variable to 1-byte char variables.
- Assume the variable is declared and defined as
long double var;
First: I created 8 char variables and tried to use shifting operator to break down the variable.
char byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8;
byte1=var;
This works fine. Lowest byte of the var is assigned to byte1 successfully.
byte2=var>>8;
error: #1460: expression must have integral or fixed-point type
Compiler gives error because of shifting operator, it doesnt work on float, double, long double. Also bitwise operators such as & and | cannot be used on these kind of variables too.
Second: I tried to use a pointer to read long double variable byte-by-byte but i then noticed that long double pointer also points all 8-byte address, so i get all 8-byte value when i read by using the pointer. And unfortunately 1-byte char pointer cannot be assiged to adress of a long double variable as below, gives error.
char *dp=(char*)&var;
or
dp=&var;
Conclusion: I need some help to save 8-byte variable as byte-by-byte into any other 1-byte variables :) I'd be glad for any ideas.
#include <stdio.h> int main(void) { long double ld = 123.456; unsigned char *p = (unsigned char *)&ld; int i; for(i=0; i<sizeof(long double); i++) printf("%02X\n",p[i]); while(1); }