C_lecture_4

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

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

View Full Document Right Arrow Icon
CS 11 C track: lecture 4 Last week: arrays This week: Recursion Introduction to pointers
Background image of page 1

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

View Full DocumentRight Arrow Icon
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!
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Recursion (2) int factorial (int n) { assert(n >= 0); if (n == 0) { return 1; /* Base case. */ } else { /* Recursive step: */ return n * factorial (n - 1); } }
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Pointers (2) 10 0x123aa8 i name address contents int i = 10; int * j = & i; 0x123aa8 0x123aab j /* j "points" to i */
Background image of page 7

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

View Full DocumentRight Arrow Icon
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);
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

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 Right Arrow Icon
Ask a homework question - tutors are online