CS 211:
Introduction to Computer Programming II
| Instructor: Brian M. Dennis | |
| Teaching Assistants: | |
| Tom Lechner, Bin Lin | |
| http://www.cs.northwestern.edu/~bmd/cs211/ | |
| 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; | |
| } | |
| #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"; | |
| } | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int* int_ptr = 0; | |
| int_ptr = NULL; | |
| char *c_ptr = NULL; | |
| c_ptr = 0; | |
| char c = 'A'; | |
| char *c_ptr2 = &c; | |
| c_ptr = c_ptr2; | |
| // Do some work | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int* int_ptr = 0; | |
| int_ptr = NULL; | |
| char *c_ptr = NULL; | |
| c_ptr = 0; | |
| char c = 'A', *c_ptr2 = &c; | |
| int_ptr = c_ptr; // Shouldn't compile | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int* int_ptr = 0; | |
| int_ptr = NULL; | |
| char *c_ptr = NULL; | |
| c_ptr = 0; | |
| char c = 'A', *c_ptr2 = &c; | |
| int_ptr = (int*)c_ptr; // Should compile | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int* int_ptr = 0; | |
| int_ptr = NULL; | |
| char *c_ptr = NULL; | |
| c_ptr = 0; | |
| char c = 'A', *c_ptr2 = &c; | |
| int_ptr = static_cast<int*>(c_ptr); | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int* int_ptr = 0; | |
| int_ptr = NULL; | |
| char *c_ptr = NULL; | |
| c_ptr = 0; | |
| char c = 'A', *c_ptr2 = &c; | |
| void* vptr = c_ptr2; | |
| int_ptr = (int*)static_cast<void*>(c_ptr); | |
| return 0; | |
| } |
| // Array sizes have to be constant expressions | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int cnt = 100; | |
| int a[100], b[cnt]; | |
| cin >> cnt; | |
| int b[cnt]; | |
| // Do some other stuff | |
| } | |
| // Arrays can be initialized | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| char *strings[] = { | |
| "one", "two", "three"}; | |
| for (int i = 0; i < 3; i++) { | |
| cout << strings[i] << endl; | |
| } | |
| int ints[] = { 1, 2, 3, 5, 8 }; | |
| return 0; | |
| } | |
Arrays of
pointers
& Pointers to pointers
| // Arrays can be initialized | |
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| char *strings[] = { | |
| "one", "two", "three"}; | |
| strings[1] = "Hello, world"; | |
| for (int i = 0; i < 3; i++) { | |
| cout << strings[i] << endl; | |
| } | |
| char **s_ptr = strings; | |
| char **s_ptr = &strings[0]; | |
| } | |
| // main actually gets command line arguments | |
| #include <iostream> | |
| using namespace std; | |
| int main(int argc, char* argv[]) { | |
| for (int i = 0; i < argc; i++) { | |
| cout << argv[i] << endl; | |
| } | |
| return 0; | |
| } | |
| // main actually gets command line arguments | |
| #include <iostream> | |
| using namespace std; | |
| int main(int argc, char* argv[]) { | |
| char **t_ptr = argv; | |
| while (t_ptr < argv + argc) { | |
| for (int i = 0; (*t_ptr)[i]; i++) { | |
| cout << (*t_ptr)[i]; | |
| } | |
| cout << endl; | |
| t_ptr++; | |
| } | |
| return 0; | |
| } | |
| // main actually gets command line arguments | |
| #include <iostream> | |
| using namespace std; | |
| int main(int argc, char* argv[]) { | |
| char **t_ptr = argv; | |
| while (t_ptr < argv + argc) { | |
| char c, *t_string = *t_ptr; | |
| while (c = *t_string) { | |
| cout << c; | |
| t_string++; | |
| } | |
| cout << endl; | |
| t_ptr++; | |
| } | |
| return 0; | |
| } | |
| // nonconstant ptr to nonconstant data | |
| #include <cctype> | |
| void convertToUppercase(char *sPtr) { | |
| while (*sPtr != '\0') { | |
| if (islower( *sPtr )) { | |
| *sPtr = toupper( *sPtr ); | |
| } | |
| ++sPtr; | |
| } | |
| } |
| // nonconstant ptr to constant data | |
| void printCharacters(const char* sPtr) { | |
| for ( ; *sPtr != '\0'; sPtr++) { | |
| cout << *sPtr; | |
| *sPtr = 'A'; | |
| } | |
| } |
| // constant ptr to nonconstant data | |
| int main() { | |
| int x, y; | |
| int * const ptr = &x; | |
| *ptr = 7; | |
| ptr = &x; | |
| return 0; | |
| } |
| // constant ptr to constant data | |
| int main() { | |
| int x = 5, y; | |
| const int *const ptr = &x; | |
| *ptr = 7; | |
| ptr = &y; | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int linearSearch(const int a[], int k, int size); | |
| int main() { | |
| const int arraySize = 100; | |
| int a[arraySize]; | |
| int key; | |
| for (int i = 0; I < arraySize; i++) { | |
| a[i] = 2 * I; | |
| } | |
| cout << "Enter integer search key: "; | |
| int element = linearSearch(a, key, arraySize); | |
| if (element != -1) { | |
| cout << Found value in element: " << element << endl; | |
| } else { | |
| cout << "Value not found" << endl; | |
| } | |
| return 0; | |
| } | |
| int linearSearch(const int array[], int key, int sizeOfArray) { | |
| for (int j = 0; j < sizeOfArray; j++) { | |
| if (array[j] == key) { | |
| return j; | |
| } | |
| } | |
| return -1; | |
| } | |
| int linearSearch(const int array[], int key, int sizeOfArray) { | |
| const int *t_array = array; | |
| while (t_array < (array + sizeOfArray)) { | |
| if (*t_array++ == key) { | |
| return (t_array array 1); | |
| } | |
| } | |
| return -1; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int binarySearch(const int a[], int k, int low, int high, int size) | |
| int main() { | |
| const int arraySize = 100; | |
| int a[arraySize]; | |
| int key; | |
| for (int i = 0; i < arraySize; i++) { | |
| a[i] = 2 * i; | |
| } | |
| cout << "Enter integer search key: "; | |
| int element = binarySearch(a, key, 0, arraySize 1, arraySize); | |
| if (element != -1) { | |
| cout << Found value in element: " << element << endl; | |
| } else { | |
| cout << "Value not found" << endl; | |
| } | |
| return 0; | |
| } | |
| int binarySearch(const int a[], int searchKey, int low, int high, | |
| int size) | |
| { | |
| int middle; | |
| while (low <= high) { | |
| middle = (low + high) / 2; | |
| if (searchKey == a[middle]) { | |
| return middle; | |
| } else if (searchKey < a[middle]) { | |
| high = middle 1; | |
| } else { | |
| low = middle + 1; | |
| } | |
| } | |
| return -1; | |
| } |
| Takeaways | ||
| Array initialization | ||
| Pointer initialization | ||
| Const | ||
| Pointer arithmetic | ||
| Array / pointer duality | ||
| Reading | ||
| 4.5 4.9 | ||
| 5.5 5.10 | ||
| 2.11 2.12 | ||
| 3.10, 3.17 | ||