Week 2 Pay special attention to exercise numbers this week!

Due: MEOM of day of next class meeting. Chapter 4 - C program structure, defines, includes, header files. 4.1 (sp. attention to p.70 - how to compile programs with multiple files)

Read and Comprehend:
4.3-4.6, 4.9, 4.11

NOTE: 4.11.3 - Conditional inclusion will be important for us later, so make sure you read this and understand the concept even though there is no exercise.

Exercises:
ex1. K&R ex. 4-7
ex2. K&R ex. 4-14
NOTE: By "block structure will help" they mean it will be helpful to declare variables inside a "block" (i.e. something delimited by curly-braces). The rules of variable scope apply in C just like java. If the variable is declared inside a block, it only exists inside that block. Chapter 5 - pointers, arrays, address arithmetic, etc. Whenever people groan about C, this is the stuff they groan about. Like everything else though, once you get a feel for it, it's pretty cool.

Read and Comprehend: 5.1-5.3, 5.5, 5.6, 5.10 Exercises: I recommend writing out and compiling the short little examples in the book that are part of the reading. It will help you get a feel for things. ex3. K&R ex. 5-3 ex4. K&R ex. 5-5 (pick two of the three functions to do). ex5. Dereferencing a pointer Remember, pointer holds an address of a variable or an array object. We use the * to "deference" a pointer, ie, to access the value at that memory addresss.
Example: ex5_intptr.c text
Be sure you understand every line of ex5_intptr.c.
When you are done, uncomment the last line and rerun the program. What happens, and why? Write your answer in one or two sentences - that's what you submit for ex5. ex6. Point arithmetic We can also increment, decrement, and compare pointers. A common application is to use pi++ to increment a pointer to the next element in an array. Example: ex6_increment.c text The exercise is given in the code comments. ex7. Pointers as function arguments In some cases we use pointers as function arguments when we want to modify the values of those arguments. This is known as call by reference. Complete this example so that the swap2 function works! ex7_swap.c  text ex8. Pointers to arrays and command line args (5.6,5.10) Write a program called "math" that accepts 3 command-line args to do basic arithmetic on 2 integers. For example: %> ./math 5 + 6 11 %> ./math 13 - 6 7 %> ./math 7 * 8 56 etc. If you want to get fancier, you can have it accept an arbitrary number of arguments and know order of operations. E.g. %> ./math 7 * 8 + 25 * 2 - 8 + 2 100

 

How to Submit

**Exercises 5,6,7 borrowed from Sonja Waxmonsky's (University of Chicago) C tutorials without permission.