Jump to content

Problema con interi in un array di char


Le085

Recommended Posts

Devo fare una funzione che mi inserisce dei dati in un array di char (che poi invierò su seriale)

 

ciò che devo fare è questo

 

| T | P | 2 ¦ 3 | ....

in pratica nelle prime due posizioni ci sono dei caratteri, mentre nelle due seguenti ci devo mettere un intero di due byte (uno short).

altrove.. unsigned char tempoMsg[9];

createTempoMsg(tempoMsg)

 

void createTempoMsg(unsigned char tempoMsg[])

{

tempoMsg[0]=T_SIGN;

tempoMsg[1]=P_SIGN;

unsigned short* tempoMsgLength=(unsigned short*)(&tempoMsg[2]);

*tempoMsgLength=6;

...

}

 

facendo così però mi va a modificare tempoMsg[1] e ci mette 6;

Ho scoperto quindi che shiftando tutto di 1 e quindi scrivendo:

unsigned short* tempoMsgLength=(unsigned short*)(&tempoMsg[3]);

lo mette al posto giusto: ciò vuol dire che il puntatore punta all'ultima cella di memoria e non alla prima? che arcano è mai questo? ha a che fare con il big endian?

il problema è che seguendo questa logica (ovvero che i puntatori puntano all'ultima cella, il resto del programma non va e vado a leggere celle di memorie non valide...

 

idee?

PS: sto compilando con un gcc per piattaforme ARM con linux

Link to comment
Share on other sites



Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...