Pergunta

I have a value I receive in an unsigned byte array that I would like to dereference as a float. Take ptr as a uint8_t* pointing at a byte array of four values 0,0,0xCD,0x42 (this is a little endian system).

float val = *(float*)ptr;

is return -1.34e8

When I type:

*(float*)ptr;

In the expressions window after hitting a break point in that section of code, it gives me 102.5 as expected. Further, when I type:

*(float*)(ptr - 1);

I get the incorrect -1.34e8, as if the compiler used ptr - 1 instead of what I typed.

I'm confused - am I missing something here?

Foi útil?

Solução

Basically the deref is correct.

If you printf("%p", ptr); do you get an address that is maybe not 4-byte aligned (or at least 2-byte aligned)? This may be necessary on some platforms.

For test, just pass the value of a real float in the uint8_t*, e.g.

float f= 102.5; 
yourfunct((uint8_t*)&f); 

and see if that works.

Outras dicas

*(float*)(ptr - 1); 

is the same as this

ptr--; //move the pointer to point to charackter in front of the previous one
*(float*)ptr;

Is this your intention ? Or do you just want to subtract 1 from the value that is being pointed to be ptr.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top