18.08.2013 Views

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

Dalla A alla Z passando per C - Robotica

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

struct complex z1, z2, z3, z4, ris;<br />

ris = piu(<strong>per</strong>(z1, z2), <strong>per</strong>(z3, z4));<br />

è l’implementazione dell’espressione complessa<br />

7.6 Union<br />

ris = z1z2 + z3z4<br />

Oltre alle strutture in C è possibile anche definire le union la cui definizione è molto simile a<br />

quella delle struct.<br />

union union1 {<br />

short intero;<br />

char vet[2];<br />

} un1, un2;<br />

A differenza delle strutture con le union si riserva spazio in memoria sufficiente solo <strong>per</strong> il dato<br />

più grande fra i vari campi descritti.<br />

Esempio:<br />

un1.vet[0]=’0’; un1.vet[1]=’1’;<br />

printf("%x", un1.intero);<br />

su alcuni sistemi viene stampato 3031 su altri 3130.<br />

ATTENZIONE: generalmente un codice che contiene union non è portabile.<br />

7.7 Interi indipendenti d<strong>alla</strong> piattaforma<br />

Per rendere un programma indipendente d<strong>alla</strong> piattaforma, e quindi d<strong>alla</strong> dimensione del tipo<br />

intero, sono applicabili diverse soluzioni.<br />

Il kernel Linux definisce (in ) i seguenti tipi di dimensione e segno (unsigned<br />

o signed) noti:<br />

u8 s8 u16 s16<br />

u32 s32 u64 s64<br />

Lo standard C99 definisce i seguenti tipi di dimensione e segno noto, il cui uso non è ancora<br />

molto diffuso:<br />

uint8_t int8_t uint16_t int16_t<br />

uint32_t int32_t uint64_t int64_t<br />

intptr_t<br />

L’ultimo tipo elencato è un intero della stessa dimensione di un puntatore (in pratica unsigned<br />

long).<br />

85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!