# Pause for Breath --- CS 130 // 2021-09-22 ## Administrivia - Quiz 1 returned on Gradescope + Let me know if you have any questions! - Assignment 2 is due tomorrow night # Exam 1 - Monday, September 27th ## Exam 1 - **Format of the exam**: + Written, in-class, 75-minute exam + Closed textbook/notes/internet + Can bring a 8.5 x 11, single-sided "cheat sheet" - **Topics**: + Basic CLI usage + C programming fundamentals * Arrays / pointers / strings / structs / IO * Stack memory / heap memory / malloc / free # Questions ## ...about anything? # Pointer Arithmetic ## Pointer Arithmetic - You can do arithmetic on pointers! ```c int arr[5] = {100, 200, 300, 400, 500}; int *p1 = arr; int *p2 = &arr[2]; int *p3 = p2 + 1; p2--; printf("%d %d %d\n", *p1, *p2, *p3); ``` - What do you guess the output will be? ## Exercise 1: Whatzitdo? - Draw the **state of the stack/heap** of the program execution at the specified position ```c void foo(int *p) { int x = 11, y = 13; // <========= THIS IS THE SPOT *p = x + y; } int main() { int x = 5, y = 7, sum = 0; int *p = &x; foo(&sum); *p = x + y; printf("%d %d %d %d\n", x, y, sum, *p); return 0; } ``` ## Exercise 1: Whatzitdo? - After the code finishes, what is printed? ```c void foo(int *p) { int x = 11, y = 13; // <========= THIS IS THE SPOT *p = x + y; } int main() { int x = 5, y = 7, sum = 0; int *p = &x; foo(&sum); *p = x + y; printf("%d %d %d %d\n", x, y, sum, *p); return 0; } ``` ## Exercise 2: String Trimming - In Python there is a string method called `trim` that removes spaces around a string: ```py s1 = " abc " s2 = s1.trim() # "abc" ``` - Write a C function that trims on the right: ```c // assume that s contains at least one non-space char void trim_right(char *s) ``` - It should do the trimming "in place" ```c char s[10] = "abc "; trim_right(s); // becomes "abc" ``` ## Exercise 3: Spot the Errors - Consider the following C code ```c // Copies the string s, returning an entirely new string // with the same contents char *copy(char *s) { int n = strlen(s); char arr[n]; for (int i = 0; i < n; i++) { arr[i] = s[i]; } return arr; } ``` - Anyone who uses the code above will almost certainly encounter a **segmentation fault**. Why? ## Exercise 3: Spot the Errors - I've fixed the dangling pointer error now: ```c // Copies the string s, returning an entirely new string // with the same contents char *copy(char *s) { int n = strlen(s); char *arr = malloc(n * sizeof(char)); for (int i = 0; i < n; i++) { arr[i] = s[i]; } return arr; } ``` - Anyone who uses this may encounter some unexpected behavior. Why? ## Exercise 3: Spot the Errors - I've fixed the termination character error: ```c char *copy(char *s) { int n = strlen(s); char *arr = malloc((n + 1) * sizeof(char)); for (int i = 0; i < n; i++) { arr[i] = s[i]; } arr[i] = '\0'; return arr; } ``` - Now suppose I use it: ```c char s1[10] = "abc"; char *s2 = copy(s1); ``` - What do I need to do with `s2` when I am done? ## Exercise 4: Structs - Suppose we have the following struct: ```c typedef struct { char name[20]; int age; } PERSON; ``` - Write a function that takes in an array of people and returns the youngest person in the array ## Exercise 5: - Write a **recursive** function that computes the sum of an array of integers - What would the prototype of the function be? ```c int sum(int *arr, int n) ``` - Remember to think about the **base case** and **recursive case**