|
1
|
- Instructor: Brian M. Dennis
- Teaching Assistants:
- Tom Lechner, Bin Lin
- http://www.cs.northwestern.edu/~bmd/cs211/
|
|
2
|
- void swap(int x, int y) {
- int old_x = x;
- x = y;
- y = old_x;
- }
- int main() {
- int x = 10, y = 20;
- swap(x, y);
- cout << "x: " << x;
- cout << " y: " << y;
- cout << "\n";
- return 1;
- }
|
|
3
|
- void swap(int& x, int &y) {
- int old_x = x;
- x = y;
- y = old_x;
- }
- int main() {
- int x = 10, y = 20;
- swap(x, y);
- cout << "x: " << x;
- cout << " y: " << y;
- cout << "\n";
- return 1;
- }
|
|
4
|
- int* swap(int* x_ptr, int *y_ptr) {
- int* z_ptr, *z;
- int old_x = *x_ptr;
- *x_ptr = *y_ptr;
- *y_ptr = old_x;
- return y_ptr;
- }
- int main() {
- int x = 10, y = 20;
- swap(&x, &y);
- cout << "x: " << x;
- cout << " y: " << y;
- cout << "\n";
- return 1;
- }
|
|
5
|
|
|
6
|
- Array of bytes
- Byte == 8 bits
- Often written in Hex, base 16
- Every variable
- Some storage in memory
- A type
- A size
- ints = 4 bytes
- floats = 4 bytes
- doubles = 8 bytes
- char = 1 byte
- pointer (of any type) = 4 bytes
|
|
7
|
- int main() {
- int x = 100;
- int z = 3;
- x = x + 1;
- // The following only work on lvalues
- x += 1; // Add 1 to x
- x += z, x += z * z; // Add 3, then add 9
- x++; // Increment x, ret oldx
- ++x; // Increment x, ret newx
- }
|
|
8
|
- int fib_pure_iter(int n) {
- int a = 1;
- int b = 0;
- int old_a = 1;
- for (int i = 0; i < n; i += 1)
- {
- old_a = a;
- a = a + b;
- b = old_a;
- }
- return b;
- }
|
|
9
|
- int fib_pure_iter(int n) {
- int a = 1;
- int b = 0;
- int old_a = 1;
- for (int i = 0; i < n; i++)
- {
- old_a = a;
- a = a + b;
- b = old_a;
- }
- return b;
- }
|
|
10
|
- int main() {
- int x = 100;
- int z = 4;
- x = x - 1;
- // The following only work on lvalues
- x -= 1; // Subtract 1 from x
- x -= z, x -= z * z; // Sub 4, then sub 16
- x--; // Decrement x, ret oldx
- --x; // Decrement x, ret newx
- }
|
|
11
|
- int fib_pure_iter(int n) {
- int a = 1;
- int b = 0;
- int old_a = 1;
- while (0 < n--) {
- old_a = a;
- a = a + b;
- b = old_a;
- }
- return b;
- }
|
|
12
|
- // Arrays: homogeneous, sequential,
- // indexed, contiguous storage, zero based
- int main() {
- int int_array[10]; // Array of 10 integers
- // 40 bytes of storage
- double double_array[5]; // Array of 5 doubles
- // 20 bytes of storage
- char char_array[10]; // Array of 10 chars
- // 10 bytes of storage
- }
|
|
13
|
- #include <iostream>
- using namespace std;
- int fib(int n);
- int main() {
- int fib_vals[10];
- for(int i = 0; i < 10; i++) {
- fib_vals[i] = fib(i);
- }
- int j = 10;
- while(j--) {
- cout << "Fib: " << j << " = "
- << fib_vals[j]
<< "\n";
- }
- }
|
|
14
|
- #include <iostream>
- using namespace std;
- int main() {
- char hw = "Hello, World";
- for (int j = 0; hw[j]; j++) {
- cout << hw[j];
- }
- cout << "\n";
- for (int k = 12; 0 <= k; k--) {
- cout << hw[k];
- }
- cut << "\n";
- }
|
|
15
|
- Bring the source code
- Trim to smallest broken example
- What are you trying to get done?
- How is your code supposed to be doing it?
- How did you run your code?
- What did you expect?
- What did you get?
|
|
16
|
- Takeaway
- Pointer basics
- Array basics
- Reading
|