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 {<br />

int x;<br />

int y;<br />

} punto;<br />

punto.x = 33;<br />

punto.y = 44;<br />

è stata definita una variabile di nomepunto, questo nome corrisponde ad una porzione di memoria<br />

in grado di conservare due dati (campi) di tipo int a cui si è dato il nome di x e y.<br />

7.5.1 Altri esempi di uso delle strutture<br />

Nelle applicazioni matematiche può essere utile definire una struttura <strong>per</strong> i numeri complessi 2 ,<br />

<strong>per</strong> esempio come segue:<br />

struct complex {<br />

float real;<br />

float imm;<br />

};<br />

e quindi delle funzioni che o<strong>per</strong>ano su numeri complessi, che abbiano come parametri strutture,<br />

ma soprattutto possano ritornare come valore una struttura:<br />

struct complex <strong>per</strong>(struct complex z1, struct complex z2)<br />

{<br />

struct complex ris;<br />

}<br />

ris.real = z1.real * z2.real -<br />

z1.imm * z2.imm;<br />

ris.imm = z1.real * z2.imm +<br />

z1.imm * z2.real;<br />

return ris;<br />

struct complex piu(struct complex z1, struct complex z2)<br />

{<br />

struct complex ris;<br />

}<br />

ris.real = z1.real + z2.real;<br />

ris.imm = z1.imm + z2.imm;<br />

return ris;<br />

e quindi utilizzare, <strong>per</strong> esempio come segue:<br />

2 L’uso di numeri complessi è talmente diffuso in applicazioni matematiche che nell’ultima revisione del<br />

linguaggio C, il C99, il tipo complesso è stato introdotto come tipo nativo.<br />

84

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

Saved successfully!

Ooh no, something went wrong!