recap: pointers - lec.inf.ethz.ch · step-by-step example. 7. a. 4. 8. int i = 5; int* p; p =...
TRANSCRIPT
![Page 1: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/1.jpg)
Recap: Pointers
int* int& *p &i &*&* ** *** *
![Page 2: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/2.jpg)
Step-by-Step Example
A0 A1 A2 A3 A4 A5
![Page 3: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/3.jpg)
Step-by-Step Example
5A0 A1 A2 A3 A4 A5
i
int i = 5;
![Page 4: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/4.jpg)
Step-by-Step Example
5A0 A1 A2 A3 A4 A5
i p
int i = 5;
int* p;
![Page 5: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/5.jpg)
Step-by-Step Example
5 A0
int i = 5;
int* p;
p = &i;
A0 A1 A2 A3 A4 A5
i p
![Page 6: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/6.jpg)
Step-by-Step Example
57 A0
int i = 5;
int* p;
p = &i;
*p = 7;
A0 A1 A2 A3 A4 A5
i p
![Page 7: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/7.jpg)
Step-by-Step Example
7 A0
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
A0 A1 A2 A3 A4 A5
i p
![Page 8: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/8.jpg)
Step-by-Step Example
7 A0 8
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
int j = *p + 1;
A0 A1 A2 A3 A4 A5
i p j
![Page 9: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/9.jpg)
Step-by-Step Example
7 A04 8
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
int j = *p + 1;
p = &j;
A0 A1 A2 A3 A4 A5
i p j
![Page 10: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/10.jpg)
Step-by-Step Example
7 A4 8
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
int j = *p + 1;
p = &j;
cout << *p; // 8
A0 A1 A2 A3 A4 A5
i p j
![Page 11: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/11.jpg)
Step-by-Step Example
7 A4 89
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
int j = *p + 1;
p = &j;
cout << *p; // 8
(*p)++;
A0 A1 A2 A3 A4 A5
i p j
![Page 12: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/12.jpg)
Step-by-Step Example
7 A45 9
int i = 5;
int* p;
p = &i;
*p = 7;
cout << i; // 7
int j = *p + 1;
p = &j;
cout << *p; // 8
(*p)++;
p++;
A0 A1 A2 A3 A4 A5
i p j
![Page 13: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/13.jpg)
Step-by-Step Example
5 A0
int i = 5;
int* p = &i; A0 A1 A2 A3 A4 A5
i p
![Page 14: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/14.jpg)
Step-by-Step Example
5 A0
int i = 5;
int* p = &i;
cout << p; // A0
A0 A1 A2 A3 A4 A5
i p
![Page 15: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/15.jpg)
Step-by-Step Example
5 A0
int i = 5;
int* p = &i;
cout << p; // A0cout << *p;// 5
A0 A1 A2 A3 A4 A5
i p
![Page 16: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/16.jpg)
Step-by-Step Example
5 A0
int i = 5;
int* p = &i;
cout << p; // A0cout << *p;// 5
cout << &p;// A2
A0 A1 A2 A3 A4 A5
i p
![Page 17: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/17.jpg)
Recap: Pointers vs. References
![Page 18: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/18.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i; Declaration and initialisation
![Page 19: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/19.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i;
*p = 0; // i = 0 r = 0; // i = 0
Declaration and initialisation
Writing to underlying variable
![Page 20: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/20.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i;
*p = 0; // i = 0 r = 0; // i = 0
cout << *p; // 0 cout << r; // 0
Declaration and initialisation
Writing to underlying variable
Reading underlying variable
![Page 21: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/21.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i;
*p = 0; // i = 0 r = 0; // i = 0
cout << *p; // 0 cout << r; // 0
int* p; int& r;p = &i; r = i;
Declaration and initialisation
Writing to underlying variable
Reading underlying variable
References must be initialisedimmediately
![Page 22: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/22.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i;
*p = 0; // i = 0 r = 0; // i = 0
cout << *p; // 0 cout << r; // 0
int* p; int& r;p = &i; r = i;
int* p = &i; int& r = i;p = &j; r = j;
Declaration and initialisation
Writing to underlying variable
Reading underlying variable
References must be initialisedimmediately
References themselves cannot be changed (“redirected”)
![Page 23: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/23.jpg)
Pointers vs. References
int i = 5;
int* p = &i; int& r = i;
*p = 0; // i = 0 r = 0; // i = 0
cout << *p; // 0 cout << r; // 0
int* p; int& r;p = &i; r = i;
int* p = &i; int& r = i;p = &j; r = j;
int* p = &i; int& r = i;cout << &p; cout << &r;
Declaration and initialisation
Writing to underlying variable
Reading underlying variable
References must be initialisedimmediately
References themselves cannot be changed (are always const)
References don’t have addresses(they are just aliases of sth. else)
![Page 24: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/24.jpg)
Why Pointers
and References?
![Page 25: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/25.jpg)
Why Pointers and References?
Historical language development:
• C: 1969; has pointers
• C++: 1983• Inherited pointers from C
(backwards compatibility)• Added references to support operator overloading
(i.e. << from streams; later in the course)
![Page 26: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/26.jpg)
Why Pointers and References?
Historical language development:
• C: 1969; has pointers
• C++: 1983• Inherited pointers from C
(backwards compatibility)• Added references to support operator overloading
(i.e. << from streams; later in the course)
Rule of thumb: prefer references over pointers
• References are more restricted harder to make mistakes
• Reference syntax is nicer (r vs *p) easier to read
![Page 27: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/27.jpg)
Why Pointers and References?
When do we need pointers?
• Fast(er) iteration over data: e.g. arrays (last week) and containers (today)
![Page 28: Recap: Pointers - lec.inf.ethz.ch · Step-by-Step Example. 7. A. 4. 8. int i = 5; int* p; p = &i; *p = 7; cout](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f20587b2f7754786174ecbc/html5/thumbnails/28.jpg)
Why Pointers and References?
When do we need pointers?
• Fast(er) iteration over data: e.g. arrays (last week) and containers (today)
• Complex data structures that change over time (later in the course): e.g. graphs
A
B
D
E F
CA
B
D
E F
C