C_lecture_4

# C_lecture_4 - CS 11 C track: lecture 4 Last week: arrays...

This preview shows pages 1–9. Sign up to view the full content.

CS 11 C track: lecture 4 Last week: arrays This week: Recursion Introduction to pointers

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Lab 4 Harder than previous labs One non-obvious trick hints on web page email me if get stuck Support code supplied for you Read carefully!
Recursion (1) Should be familiar from CS 1 Recursive functions call themselves Useful for problems that can be decomposed in terms of smaller versions of themselves

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion (2) int factorial (int n) { assert(n >= 0); if (n == 0) { return 1; /* Base case. */ } else { /* Recursive step: */ return n * factorial (n - 1); } }
Recursion (3) factorial (5) --> 5 * factorial (4) --> 5 * 4 * factorial (3) --> 5 * 4 * 3 * factorial (2) --> 5 * 4 * 3 * 2 * factorial (1) --> 5 * 4 * 3 * 2 * 1 * factorial (0) --> 5 * 4 * 3 * 2 * 1 * 1 --> 120

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pointers (1) Address: A location in memory where data can be stored e.g. a variable or an array Address of variable x is written &x Pointer: A variable which holds an address
Pointers (2) 10 0x123aa8 i name address contents int i = 10; int * j = & i; 0x123aa8 0x123aab j /* j "points" to i */

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pointers (3) int i = 10; int *j = &i; printf("i = %d\n", i); printf("j = %x\n", j); printf("j points to: %d\n", * j);
This is the end of the preview. Sign up to access the rest of the document.

## C_lecture_4 - CS 11 C track: lecture 4 Last week: arrays...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online