Ho la seguente struttura e la funzione "getter" che ritorna a
Cast a un intero senza segno:Casting tipi di puntatore su diverse architetture
struct s {
uint32_t a;
};
void get_a(struct s *st, unsigned *ret)
{
*ret = (unsigned)st->a;
}
Il seguente codice viene eseguito:
struct s st;
uint16_t x;
st.a = 1;
get_a(&st, (unsigned *)&x);
e per x86_64, i686, armv7hl, ppc64le e altre architetture x == 1
, ma per ppc64 x == 0
. Perchè è questo? Poco - big-endian?
Impostare a a 0x12345678. Le macchine little-endian restituiranno 0x5678, big-endian 0x1234. Per mantenere lo stesso comportamento su tutte le macchine usa le funzioni htonl/htons/ntohl/ntohs. – nsilent22