class: middle, center # Lecture 7 ### 4 October 2021 .smaller[ Admin Matters
Unit 15: **Pointer**
Unit 16: **Call-by-Reference**
Unit 17: **Heap**
Unit 18: **String**
] --- class: middle, center # 💪 **121** quiz questions
**46** programming problems
**24** problem set questions --- class: middle ### Assignment 4 and Quiz 7 - Due Wednesday night (23:59) --- class: middle ### Assignment 5 onwards - Write per-function documentation for your code --- class: middle ### Tutorial 6 - Problem Sets 15-18 --- class: middle ### Lab 6 - Exercise on call-by-reference and dynamically allocated arrays. --- class: middle background-image: url(figures/meme/explain_pointer.jpg) --- class: middle ### Variable - a place in memory that holds one or more values - we can access a variable through its name or its memory address --- class: middle ### Address-of operator `&` - if `x` is the name of a variable, `&x` is its memory address. - if `x` as the type `T`, `&x` has the type `*T` (pointer to `T` / address of `T`) --- class: middle ### Dereference operator `*` - if `x` is a memory address, `*x` is the content at memory address `x`. - if `x` as the type `*T`, `*x` has the type `T` --- class: middle ### Pointers must be of the same type ```C double p = 3.1415926; long radius = 5; double *addr; addr = &p; // ok addr = &radius; // not ok ``` (`void *` is special) --- class: middle ### We cannot change the address of a variable ```C long x = 1; long y = 2; &x = &y; ``` --- class: middle ### Pointer arithmetic ```C long x = 1; long *ptr = &x; ptr += 1; ``` --- class: middle,center `a[i]` is just another notation for `*(a + i)` --- class: middle ### Pointers are variables too! ```C long x; long *ptr = &x; long **ptrptr = &ptr; ``` --- class: middle ### The `NULL` Pointer A special pointer value that says a pointer points to "nothing". --- class: middle,center ## Call by Reference (non-array variables) --- class: middle ## Swap .smaller[ ```C void swap(long *a, long *b) { long temp = *a; *a = *b; *b = temp; } int main() { long a = 10; long b = -4; swap(&a, &b); } ``` ] --- class: middle,center ## Heap --- class: middle - Heap: another region of memory that a program can utilize - Program explicitly "borrows" memory space for use - Program must "return" the memory space after use - Lifetime of content stored on heap can exceed the lifetime of the function that allocates it --- class: middle,center | Size Known in Advanced | Size Determined During Run-time -------|-----------------------|----------------------------------- Stack | Fixed-size Array | VLA (stay away) Heap | stay away | `malloc` / `calloc` --- class: middle,center ## Characters and Strings --- class: middle,center ### `char` literals `'A'`, `'a'`, `'4'`, `'0'`, `'*'`, `'\n'`, `'\\'` .tiny[ A literal is a constant value in C program that cannot be changed (as opposed to a variable). ] --- class: middle,center ```C 'A' + 1 == 'B' ``` --- class: middle ### String - An array of `char` - Terminated with `'\0'` .small[ ```C char str[5] = { 'a', 'b', 'c', 'd', '\0' }; ``` is the same as ```C char str[5] = "abcd"; ``` ] --- class: middle ### String Literals Stored in read-only region in memory ```C char *str1 = "Hello"; char str2[5] = "Hello"; ``` --- class: middle ### Important Rules - Do not access memory space you are not allowed to. - Always return memory space that you borrow. --- class: middle ### Homework - Problem Set 15-18 - Assignment 4 - Quiz 7 - Exercise 8 --- class: bottom .tiny[ Version: 1.1 Last Updated: Mon Oct 4 17:08:27 +08 2021 ]