px[i] <=> (*px + i) void strcpy(char *dst, char *src) { while(*src != '\0') { *dst = *src; dst++; src++; } } int main() { char s[] = "hello\n"; char d[10]; strcpy(d, s); cout << d << endl; return 0; } void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; } int main() { int x = 1, y = 2; swap(&x, &y); cout << x << " " << y << endl; return 0; } * Arrays of pointers int main() { char *strings[] = {"hello\n", "goodbye\n", "ciao\n"}; for(i = 0; i < 3; i++) { cout << strings[i]; } } * Arguments to your program #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) { cout << argv[i] << endl; } } * Dynamic memory management #include int main() { int *p = new int; *p = 5; cout << *p << endl; } * Stack vs. Heap * Dynamically allocated arrays int main() { int i; int n; int *nums; cout << "How many numbers do you want to sum? "; cin > n; nums = new int[n]; for(i = 0; i < n; i++) { cout << "Enter number " << i + 1 << ": "; cin >> nums[i]; } cout << "The sum is " << sum(nums, n) << endl; return 0; } * Once you allocate something, it doesn't get deallocated until the program exits or you deallocate it. delete x; delete [] nums; * Always delete any dynamically allocated memory, or you might run out. int main() { while(true) { int *x = new int; } return 0; } int main() { while(true) { int *x = new int; delete x; } return 0; } * structures * create your own data type, combination of other types struct point { double x; double y; }; access individual elements using the '.' operator struct point p; p.x = 0.0; p.y = 0.0; void translate(struct point& p, int x, int y) { p.x += x; p.y += y; } struct employee { char first[30]; char last[30]; char ssn[10]; double salary; }; void printEmployee(struct employee emp) { cout << "First name: " << emp.first << endl; cout << "Last name: " << emp.last << endl; cout << "SSN: " << emp.ssn << endl; cout << "Salary: $" << emp.salary << endl; } * pointers to structs struct employee *emp = new struct employee; to access a field in the pointer: (*emp).salary or emp->salary struct employee *createEmp(char *first, char *last, char *ssn, double salary) { struct employee *emp = new struct employee; strcpy(emp->first, first); strcpy(emp->last, last); strcpy(emp->ssn, ssn); emp->salary = salary; return emp; } structures can contain pointers to structures of the same type: struct linkedList { struct linkedListNode *head; struct linkedListNode *tail; }; struct linkedListNode { int key; struct linkedListNode *next; } struct linkedList *createLinkedList() { struct linkedList *l = new struct linkedList; l->head = NULL; l->tail = NULL; return l; } struct linkedListNode *createLinkedListNode(int key) { struct linkedListNode *n = new struct linkedListNode; n->next = NULL; n->key = key; return n; } void insertFront(struct linkedList *list, int key) { struct linkedListNode *n = createLinkedListNode(key); n->next = list->head; list->head = n; if(list->tail == NULL) { list->tail = n; } } void insertBack(struct linkedList *list, int key) { struct linkedListNode *n = createLinkedListNode(key); list->tail = n; if(list->tail == NULL) { list->head = n; } else { list->tail->next = n; } } void printList(struct linkedList *list) { struct linkedListNode *cur; for(cur = list->head; cur != NULL; cur = cur->next) { cout << cur->key << " "; } } void freeList(struct linkedList *list) { struct linkedListNode *cur, *temp; for(cur = list->head; cur; cur = temp) { temp = cur->next; delete cur; } } int main() { struct linkedList *list = createLinkedList(); insertFront(list, 1); insertFront(list, 2); insertFront(list, 3); insertFront(list, 4); printList(list); freeList(list); return 0; }