c - reading back a float number -


i using microchip c18 , have function splits float in 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 serial eeprom.

if wanted read 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 function return float variable?. i'm devoid of hardware , simulation tools right now.

i trust i'm clear on question. edit:

while doing so. compiler mismatch error occurs on assigning char float..how remove error?

the cleanest way *(char*)(fptr+i) = eepromread(i);. want offset initial pointer, cast pointer character, dereferenced.

also, @ least compiler (gcc) balks @ first assignment. need more fptr = (char*)(&f); pointer float assignment-compatible.

double-check, though, sure eepromread() gives bytes in order expect them. should, since ieee754 independent of byte-ordering, number of "clever enhancements" hidden in embedded c libraries fill pretty big book.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -