![Page 1: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/1.jpg)
IEIM 2019-2020
Esercitazione V“Puntatori”
Alessandro A. [email protected] - www.alessandronacci.it
1
![Page 2: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/2.jpg)
Puntatori e memoriaEsercizio 2
2
![Page 3: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/3.jpg)
Esercizio 2: Puntatori e memoria
3
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
DATO UN PROGRAMMA C, COME SI COMPORTA LA MEMORIA?
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 4: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/4.jpg)
Esercizio 2: Puntatori e memoria
4
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
DATO UN PROGRAMMA C, COME SI COMPORTA LA MEMORIA?
ATTENZIONE!Il comportamento della memoria mostrato in questo esercizio
non è del tutto coerente con quanto avviene su un reale calcolatore. L’esempio mostrato è però funzionale alla spiegazione del comportamento dei puntatori in C.
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 5: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/5.jpg)
Esercizio 2: Puntatori e memoria
5
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
DATO UN PROGRAMMA C, COME SI COMPORTA LA MEMORIA?
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 6: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/6.jpg)
Esercizio 2: Puntatori e memoria
6
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 7: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/7.jpg)
Esercizio 2: Puntatori e memoria
7
30
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 8: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/8.jpg)
Esercizio 2: Puntatori e memoria
8
30
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b~
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 9: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/9.jpg)
Esercizio 2: Puntatori e memoria
9
30
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 10: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/10.jpg)
Esercizio 2: Puntatori e memoria
10
3
3~~~~
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 11: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/11.jpg)
Esercizio 2: Puntatori e memoria
11
3
3~~~~~
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
c
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 12: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/12.jpg)
Esercizio 2: Puntatori e memoria
12
3
3~13~~~
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
c
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 13: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/13.jpg)
Esercizio 2: Puntatori e memoria
13
3
3~13~~5
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
c
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 14: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/14.jpg)
Esercizio 2: Puntatori e memoria
14
3
3~13~~53
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
c
d
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 15: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/15.jpg)
Esercizio 2: Puntatori e memoria
15
3
3~13~~53~
0
1
2
3
4
5
6
7
8
9
IND. CONTENUTO
int main(){int a = 3;printf(“%d”, &a);int* b;b = &a;
int mat[2][2];int* c;mat[0][1] = 13;c = &(mat[1][0]);int d = *b;int e;
}
a
b
mat
mat
mat
mat
mat
c
d
e
0
Indicare nella tabella come il programma C mostrato modifica lo stato della memoria del calcolatore.
![Page 16: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/16.jpg)
Le funzioni
• Le funzioni sono “blocchi” di codice che prendono in ingresso dei valori tramite i parametri e restituiscono, dopo della computazione, un risultato.
16
q = f(x,y,z)
xyz
q
![Page 17: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/17.jpg)
Le funzioni
• Le funzioni sono “blocchi” di codice che prendono in ingresso dei valori tramite i parametri e restituiscono, dopo della computazione, un risultato.
16
q = f(x,y,z)
xyz
q
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
![Page 18: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/18.jpg)
Le funzioni
17
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
![Page 19: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/19.jpg)
Le funzioni
17
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
Nome
![Page 20: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/20.jpg)
Le funzioni
17
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
Parametri in ingressocon tipo
Nome
![Page 21: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/21.jpg)
Le funzioni
17
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
Parametri in ingressocon tipoTipo dato
in uscita(void se non
Nome
![Page 22: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/22.jpg)
Le funzioni
17
int foo(int x, int y, int z){
int q;
// codice vario // che effettua calcoli
return q;
}
Parametri in ingressocon tipoTipo dato
in uscita(void se non
Singolo valore in uscita (in C si può restituire un solo valore
alla volta)
Nome
![Page 23: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/23.jpg)
Le funzioni con gli array
• Per alcuni motivazioni tecniche del C, con gli array ci sono alcuni “problemini”
• Semplificando, In C non è possibile fare return di un array
• In C, quando si passa in ingresso un array come parametro, le modifiche fatte su quel parametro agiscono direttamente sull’array originale passato dal chiamante.
18
![Page 24: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/24.jpg)
Le funzioni con gli array
19
![Page 25: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/25.jpg)
Le funzioni con gli array
19
void foo(int x_arr[]){
x_arr[3] = 13;return;
}
int main(){int x_arr[10];x_arr[3]= 2;
printf(“%d”,x_arr[3]); // Stampa 2
foo(x_arr);
printf(“%d”,x_arr[3]); // Stampa 13}
![Page 26: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/26.jpg)
Vediamo cosa succede in memoria
20
int somma_speciale(int d, int c){
int q = 3;return d + c + q;
}
int main(){
int a;int b;int c;
a = 2;b = 5;
c = somma_speciale(a,b);}
![Page 27: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/27.jpg)
Vediamo cosa succede in memoria
21
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio valori
• Si scriva una funzione in C che, datidue valori interi, gli restituiscescambiati
20
![Page 28: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/28.jpg)
Un esempio: funzione swap
22
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int p, int q) {int temp;temp = p; p = q;q = temp;
}
• Nel main: swap(a,b)
21
a
b
3
7
p
q
temp
3
7
![Page 29: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/29.jpg)
Un esempio: funzione swap
23
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int p, int q) {int temp;temp = p; p = q;q = temp;
}
• Nel main: swap(a,b)
22
a
b
3
7
p
q
temp
3
7
3
![Page 30: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/30.jpg)
Un esempio: funzione swap
24
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int p, int q) {int temp;temp = p; p = q;q = temp;
}
• Nel main: swap(a,b)
23
a
b
3
7
p
q
temp
7
7
3
![Page 31: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/31.jpg)
Un esempio: funzione swap
25
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int p, int q) {int temp;temp = p; p = q;q = temp;
}
• Nel main: swap(a,b)
24
a
b
3
7
p
q
temp
7
3
3
![Page 32: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/32.jpg)
Un esempio: funzione swap
26
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int p, int q) {int temp;temp = p; p = q;q = temp;
}
• Nel main: swap(a,b)
25
a
b
3
7
p
q
temp
7
3
3Al termine dell’esecuzione di swap le variabili nel main restano inalterate!
![Page 33: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/33.jpg)
Un esempio: funzione swap
27
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int *p, int *q){int temp;temp = *p; *p = *q;*q = temp;
}
• Nel main: swap(&a, &b)
26
a
b
3
7
p
q
temp
![Page 34: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/34.jpg)
Un esempio: funzione swap
28
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int *p, int *q){int temp;temp = *p; *p = *q;*q = temp;
}
• Nel main: swap(&a, &b)
27
a
b
3
7
p
q
temp
3
![Page 35: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/35.jpg)
Un esempio: funzione swap
29
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int *p, int *q){int temp;temp = *p; *p = *q;*q = temp;
}
• Nel main: swap(&a, &b)
28
a
b
7
7
p
q
temp
3
![Page 36: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/36.jpg)
Un esempio: funzione swap
30
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int *p, int *q){int temp;temp = *p; *p = *q;*q = temp;
}
• Nel main: swap(&a, &b)
29
a
b
7
3
p
q
temp
3
![Page 37: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/37.jpg)
Un esempio: funzione swap
31
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Esempio: scambio di 2 valori interi
void swap (int *p, int *q){int temp;temp = *p; *p = *q;*q = temp;
}
• Nel main: swap(&a, &b)
30
a
b
7
3
p
q
temp
3Al termine dell’esecuzione di swap le variabili nel main vengono modificate
![Page 38: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/38.jpg)
32
DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE
Fonti per lo studio + Credits
• Fonti per lo studio§ Binky Pointer Fun Video:
http://cslibrary.stanford.edu/104/
• Credits§ Gianluca Palermo
31
![Page 39: es5 - DEIB - Politecnico di Milano · DIPARTIMENTO DI ELETTRONICA E INFORMAZIONE Esempio: scambio di 2 valori interi void swap (int p, int q) {int temp; temp = p; p = q; q = temp;}](https://reader035.vdocuments.site/reader035/viewer/2022063017/5fd8f64001c7672565038b4b/html5/thumbnails/39.jpg)
33
Tutte il materiale sarà disponibile sul mio sito internet!
www.alessandronacci.it