I am using microchip c18 and i have this function which splits the float in to 4 respective bytes.And C18 follow little endianess
a[0]=*(fptr); address 0
a[1]=*(fptr+1); 1
a[2]=*(fptr+2); 2
a[3]=*(fptr+3); 3
and writes in to serial eeprom.
If i wanted to read back the float variable.
float read_float(void)
{ float f;
unsigned char *fptr;
fptr=&f;
*(fptr)=eepromread(0);
*(fptr+1)=eepromread(1);
*(fptr+2)=eepromread(2);
*(fptr+3)=eepromread(3);
return(f);
}
Will this function return the float variable?. I'm devoid of any hardware and simulation tools right now.
I trust I'm clear on my question. edit:
While doing so. a compiler mismatch error occurs on assigning char to float..How could i remove the error?
The cleanest way is
*(char*)(fptr+i) = eepromread(i);. You want the offset from the initial pointer, cast to a pointer to a character, dereferenced.Also, at least my compiler (gcc) balks at that first assignment. You need something more like
fptr = (char*)(&f);so that the pointer to the float is assignment-compatible.Double-check, though, to be sure that
eepromread()gives you the bytes in the order that you expect them. They should, since IEEE754 is independent of byte-ordering, but the number of "clever enhancements" hidden in embedded C libraries could fill a pretty big book.