CS 107 Lecture 9 C Generics Function Pointers
![CS 107, Lecture 9 C Generics – Function Pointers Reading: K&R 5. 11 This CS 107, Lecture 9 C Generics – Function Pointers Reading: K&R 5. 11 This](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-1.jpg)
![Learning Goals • Learn how to write C code that works with any data Learning Goals • Learn how to write C code that works with any data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-2.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-3.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-4.jpg)
![Generics • We always strive to write code that is as general-purpose as possible. Generics • We always strive to write code that is as general-purpose as possible.](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-5.jpg)
![Generic Swap Wouldn’t it be nice if we could write one function that would Generic Swap Wouldn’t it be nice if we could write one function that would](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-6.jpg)
![Generic Swap void swap(void *data 1 ptr, void *data 2 ptr, size_t nbytes) { Generic Swap void swap(void *data 1 ptr, void *data 2 ptr, size_t nbytes) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-7.jpg)
![Void * Pitfalls • void *s are powerful, but dangerous - C cannot do Void * Pitfalls • void *s are powerful, but dangerous - C cannot do](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-8.jpg)
![Swap Ends Let’s write a function that swaps the first and last elements in Swap Ends Let’s write a function that swaps the first and last elements in](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-9.jpg)
![Pointer Arithmetic arr + nelems – 1 Let’s say nelems = 4. How many Pointer Arithmetic arr + nelems – 1 Let’s say nelems = 4. How many](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-10.jpg)
![Swap Ends You’re asked to write a function that swaps the first and last Swap Ends You’re asked to write a function that swaps the first and last](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-11.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-12.jpg)
![Stacks • C generics are particularly powerful in helping us create generic data structures. Stacks • C generics are particularly powerful in helping us create generic data structures.](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-13.jpg)
![Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-14.jpg)
![Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-15.jpg)
![Generic Stack Structs typedef struct int_node { struct int_node *next; void *data; } int_node; Generic Stack Structs typedef struct int_node { struct int_node *next; void *data; } int_node;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-16.jpg)
![Stack Functions • int_stack_create(): creates a new stack on the heap and returns a Stack Functions • int_stack_create(): creates a new stack on the heap and returns a](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-17.jpg)
![int_stack_create int_stack *int_stack_create() { int_stack *s = malloc(sizeof(int_stack)); s->nelems = 0; s->top = NULL; int_stack_create int_stack *int_stack_create() { int_stack *s = malloc(sizeof(int_stack)); s->nelems = 0; s->top = NULL;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-18.jpg)
![Generic stack_create stack *stack_create(int elem_size_bytes) { stack *s = malloc(sizeof(stack)); s->nelems = 0; s->top Generic stack_create stack *stack_create(int elem_size_bytes) { stack *s = malloc(sizeof(stack)); s->nelems = 0; s->top](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-19.jpg)
![int_stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data; int_stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-20.jpg)
![Generic stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = Generic stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data =](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-21.jpg)
![Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-22.jpg)
![Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-23.jpg)
![Generic stack_push int main() { stack *int_stack = stack_create(sizeof(int)); add_one(int_stack); // now stack stores Generic stack_push int main() { stack *int_stack = stack_create(sizeof(int)); add_one(int_stack); // now stack stores](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-24.jpg)
![Generic stack_push void stack_push(stack *s, const void *data) { node *new_node = malloc(sizeof(node)); new_node->data Generic stack_push void stack_push(stack *s, const void *data) { node *new_node = malloc(sizeof(node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-25.jpg)
![int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-26.jpg)
![Generic stack_pop int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop Generic stack_pop int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-27.jpg)
![Generic stack_pop void *int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot Generic stack_pop void *int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-28.jpg)
![Generic stack_pop void stack_pop(stack *s, void *addr) { if (s->nelems == 0) { error(1, Generic stack_pop void stack_pop(stack *s, void *addr) { if (s->nelems == 0) { error(1,](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-29.jpg)
![Using Generic Stack int_stack *intstack = int_stack_create(); for (int i = 0; i < Using Generic Stack int_stack *intstack = int_stack_create(); for (int i = 0; i <](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-30.jpg)
![Using Generic Stack stack *intstack = stack_create(sizeof(int)); for (int i = 0; i < Using Generic Stack stack *intstack = stack_create(sizeof(int)); for (int i = 0; i <](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-31.jpg)
![Using Generic Stack int_stack *intstack = int_stack_create(); int_stack_push(intstack, 7); We must now pass the Using Generic Stack int_stack *intstack = int_stack_create(); int_stack_push(intstack, 7); We must now pass the](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-32.jpg)
![Using Generic Stack stack *intstack = stack_create(sizeof(int)); int num = 7; stack_push(intstack, &num); We Using Generic Stack stack *intstack = stack_create(sizeof(int)); int num = 7; stack_push(intstack, &num); We](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-33.jpg)
![Using Generic Stack // Pop off all elements while (intstack->nelems > 0) { printf("%dn", Using Generic Stack // Pop off all elements while (intstack->nelems > 0) { printf("%dn",](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-34.jpg)
![Using Generic Stack // Pop off all elements int popped_int; while (intstack->nelems > 0) Using Generic Stack // Pop off all elements int popped_int; while (intstack->nelems > 0)](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-35.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-36.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-37.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-38.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-39.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-40.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-41.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-42.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-43.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-44.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-45.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-46.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-47.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-48.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-49.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-50.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-51.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-52.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-53.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-54.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-55.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-56.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-57.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-58.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-59.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-60.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-61.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-62.jpg)
![Bubble Sort • Let’s write a function to sort a list of integers. We’ll Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-63.jpg)
![Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-64.jpg)
![Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-65.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-66.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-67.jpg)
![Key Idea: Generically Getting i-th Elem A common generics idiom is getting a pointer Key Idea: Generically Getting i-th Elem A common generics idiom is getting a pointer](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-68.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-69.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-70.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-71.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-72.jpg)
![A Generics Conundrum • We’ve hit a wall – there is no way to A Generics Conundrum • We’ve hit a wall – there is no way to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-73.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-74.jpg)
![Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-75.jpg)
![Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, function compare_fn) { while Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, function compare_fn) { while](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-76.jpg)
![Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-77.jpg)
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-78.jpg)
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-79.jpg)
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-80.jpg)
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-81.jpg)
![Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-82.jpg)
![Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) {. . . } int Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) {. . . } int](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-83.jpg)
![Function Pointers • Function pointers must always take pointers to the data they care Function Pointers • Function pointers must always take pointers to the data they care](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-84.jpg)
![Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-85.jpg)
![Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-86.jpg)
![Comparison Functions • Function pointers are used often in cases like this to compare Comparison Functions • Function pointers are used often in cases like this to compare](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-87.jpg)
![Function Pointers integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int Function Pointers integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-88.jpg)
![Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, int (*compare_fn)(void *a, void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, int (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-89.jpg)
![Tracing Bubble Sort int main(int argc, char *argv[]) { int nums[] = {5, 2}; Tracing Bubble Sort int main(int argc, char *argv[]) { int nums[] = {5, 2};](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-90.jpg)
![Comparison Functions • Exercise: how can we write a comparison function for bubble sort Comparison Functions • Exercise: how can we write a comparison function for bubble sort](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-91.jpg)
![String Comparison Function int string_compare(void *ptr 1, void *ptr 2) { // cast arguments String Comparison Function int string_compare(void *ptr 1, void *ptr 2) { // cast arguments](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-92.jpg)
![Function Pointer Pitfalls • If a function takes a function pointer as a parameter, Function Pointer Pitfalls • If a function takes a function pointer as a parameter,](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-93.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-94.jpg)
![Function Pointers • Function pointers can be used in a variety of ways. For Function Pointers • Function pointers can be used in a variety of ways. For](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-95.jpg)
![Function Pointers • Function pointers can be used in a variety of ways. For Function Pointers • Function pointers can be used in a variety of ways. For](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-96.jpg)
![Common Utility Callback Functions • Comparison function – compares two elements of a given Common Utility Callback Functions • Comparison function – compares two elements of a given](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-97.jpg)
![Generic Array Printing We would like to write a generic function that, given an Generic Array Printing We would like to write a generic function that, given an](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-98.jpg)
![Generics Overview • We use void * pointers and memory operations like memcpy and Generics Overview • We use void * pointers and memory operations like memcpy and](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-99.jpg)
![Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-100.jpg)
- Slides: 100
![CS 107 Lecture 9 C Generics Function Pointers Reading KR 5 11 This CS 107, Lecture 9 C Generics – Function Pointers Reading: K&R 5. 11 This](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-1.jpg)
CS 107, Lecture 9 C Generics – Function Pointers Reading: K&R 5. 11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2. 5 License. All rights reserved. Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, and others. 1
![Learning Goals Learn how to write C code that works with any data Learning Goals • Learn how to write C code that works with any data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-2.jpg)
Learning Goals • Learn how to write C code that works with any data type. • Learn how to pass functions as parameters • Learn how to write functions that accept functions as parameters 2
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-3.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers 3
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-4.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers 4
![Generics We always strive to write code that is as generalpurpose as possible Generics • We always strive to write code that is as general-purpose as possible.](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-5.jpg)
Generics • We always strive to write code that is as general-purpose as possible. • Generic code reduces code duplication, and means you can make improvements and fix bugs in one place rather than many. • Generics is used throughout C for functions to sort any array, search any array, free arbitrary memory, and more. 5
![Generic Swap Wouldnt it be nice if we could write one function that would Generic Swap Wouldn’t it be nice if we could write one function that would](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-6.jpg)
Generic Swap Wouldn’t it be nice if we could write one function that would work with any parameter type, instead of so many different versions? void void … swap_int(int *a, int *b) { … } swap_float(float *a, float *b) { … } swap_size_t(size_t *a, size_t *b) { … } swap_double(double *a, double *b) { … } swap_string(char **a, char **b) { … } swap_mystruct(mystruct *a, mystruct *b) { … } 6
![Generic Swap void swapvoid data 1 ptr void data 2 ptr sizet nbytes Generic Swap void swap(void *data 1 ptr, void *data 2 ptr, size_t nbytes) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-7.jpg)
Generic Swap void swap(void *data 1 ptr, void *data 2 ptr, size_t nbytes) { char temp[nbytes]; // store a copy of data 1 in temporary storage memcpy(temp, data 1 ptr, nbytes); // copy data 2 to location of data 1 memcpy(data 1 ptr, data 2 ptr, nbytes); // copy data in temporary storage to location of data 2 memcpy(data 2 ptr, temp, nbytes); } We can use void * to represent a pointer to any data, and memcpy to copy arbitrary bytes. 7
![Void Pitfalls void s are powerful but dangerous C cannot do Void * Pitfalls • void *s are powerful, but dangerous - C cannot do](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-8.jpg)
Void * Pitfalls • void *s are powerful, but dangerous - C cannot do as much checking! • E. g. with int, C would never let you swap half of an int. With void *s, this can happen! int x = 0 xffff; int y = 0 xeeee; swap(&x, &y, sizeof(short)); // now x = 0 xffffeeee, y = 0 xeeeeffff! printf("x = 0 x%x, y = 0 x%xn", x, y); 8
![Swap Ends Lets write a function that swaps the first and last elements in Swap Ends Let’s write a function that swaps the first and last elements in](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-9.jpg)
Swap Ends Let’s write a function that swaps the first and last elements in an array. How can we make this generic? void swap_ends_int(int *arr, size_t nelems) { swap(arr, arr + nelems – 1, sizeof(*arr)); } void swap_ends_short(short *arr, size_t nelems) { swap(arr, arr + nelems – 1, sizeof(*arr)); } void swap_ends_string(char **arr, size_t nelems) { swap(arr, arr + nelems – 1, sizeof(*arr)); } void swap_ends_float(float *arr, size_t nelems) { swap(arr, arr + nelems – 1, sizeof(*arr)); } 9
![Pointer Arithmetic arr nelems 1 Lets say nelems 4 How many Pointer Arithmetic arr + nelems – 1 Let’s say nelems = 4. How many](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-10.jpg)
Pointer Arithmetic arr + nelems – 1 Let’s say nelems = 4. How many bytes beyond arr is this? If it’s an array of… Int: adds 3 places to arr, and 3 * sizeof(int) = 12 bytes Short: adds 3 places to arr, and 3 * sizeof(short) = 6 bytes Char *: adds 3 places to arr, and 3 * sizeof(char *) = 24 bytes In each case, we need to know the element size to do the arithmetic. 10
![Swap Ends Youre asked to write a function that swaps the first and last Swap Ends You’re asked to write a function that swaps the first and last](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-11.jpg)
Swap Ends You’re asked to write a function that swaps the first and last elements in an array of numbers. Well, now it can swap an array of anything! void swap_ends(void *arr, size_t nelems, size_t elem_bytes) { swap(arr, (char *)arr + (nelems – 1) * elem_bytes, elem_bytes); } 11
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-12.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers 12
![Stacks C generics are particularly powerful in helping us create generic data structures Stacks • C generics are particularly powerful in helping us create generic data structures.](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-13.jpg)
Stacks • C generics are particularly powerful in helping us create generic data structures. • Let’s see how we might go about making a generic Stack in C. 13
![Stack Structs typedef struct intnode struct intnode next int data intnode typedef Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-14.jpg)
Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef struct int_stack { int nelems; int_node *top; } int_stack; 14
![Stack Structs typedef struct intnode struct intnode next int data intnode typedef Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-15.jpg)
Stack Structs typedef struct int_node { struct int_node *next; int data; } int_node; typedef struct int_stack { int nelems; int_node *top; } int_stack; Problem: each node can no longer store the data itself, because it could be any size! 15
![Generic Stack Structs typedef struct intnode struct intnode next void data intnode Generic Stack Structs typedef struct int_node { struct int_node *next; void *data; } int_node;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-16.jpg)
Generic Stack Structs typedef struct int_node { struct int_node *next; void *data; } int_node; typedef struct stack { int nelems; int elem_size_bytes; node *top; } stack; Solution: each node stores a pointer, which is always 8 bytes, to the data somewhere else. We must also store the data size in the Stack struct. 16
![Stack Functions intstackcreate creates a new stack on the heap and returns a Stack Functions • int_stack_create(): creates a new stack on the heap and returns a](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-17.jpg)
Stack Functions • int_stack_create(): creates a new stack on the heap and returns a pointer to it • int_stack_push(int_stack *s, int data): pushes data onto the stack • int_stack_pop(int_stack *s): pops and returns topmost stack element 17
![intstackcreate intstack intstackcreate intstack s mallocsizeofintstack snelems 0 stop NULL int_stack_create int_stack *int_stack_create() { int_stack *s = malloc(sizeof(int_stack)); s->nelems = 0; s->top = NULL;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-18.jpg)
int_stack_create int_stack *int_stack_create() { int_stack *s = malloc(sizeof(int_stack)); s->nelems = 0; s->top = NULL; return s; } 18
![Generic stackcreate stack stackcreateint elemsizebytes stack s mallocsizeofstack snelems 0 stop Generic stack_create stack *stack_create(int elem_size_bytes) { stack *s = malloc(sizeof(stack)); s->nelems = 0; s->top](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-19.jpg)
Generic stack_create stack *stack_create(int elem_size_bytes) { stack *s = malloc(sizeof(stack)); s->nelems = 0; s->top = NULL; s->elem_size_bytes = elem_size_bytes; return s; } 19
![intstackpush void intstackpushintstack s int data intnode newnode mallocsizeofintnode newnodedata data int_stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data;](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-20.jpg)
int_stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data; } new_node->next = s->top; s->top = new_node; s->nelems++; 20
![Generic stackpush void intstackpushintstack s int data intnode newnode mallocsizeofintnode newnodedata Generic stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data =](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-21.jpg)
Generic stack_push void int_stack_push(int_stack *s, int data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data; } new_node->next = s->top; s->top = new_node; s->nelems++; Problem 1: we can no longer pass the data itself as a parameter, because it could be any size! 21
![Generic stackpush void intstackpushintstack s const void data intnode newnode mallocsizeofintnode newnodedata Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-22.jpg)
Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data; } new_node->next = s->top; s->top = new_node; s->nelems++; Solution 1: pass a pointer to the data as a parameter instead. 22
![Generic stackpush void intstackpushintstack s const void data intnode newnode mallocsizeofintnode newnodedata Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-23.jpg)
Generic stack_push void int_stack_push(int_stack *s, const void *data) { int_node *new_node = malloc(sizeof(int_node)); new_node->data = data; } new_node->next = s->top; s->top = new_node; s->nelems++; Problem 2: we cannot copy the existing data pointer into new_node. The data structure must manage its own copy that exists for its entire lifetime. The provided copy may go away! 23
![Generic stackpush int main stack intstack stackcreatesizeofint addoneintstack now stack stores Generic stack_push int main() { stack *int_stack = stack_create(sizeof(int)); add_one(int_stack); // now stack stores](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-24.jpg)
Generic stack_push int main() { stack *int_stack = stack_create(sizeof(int)); add_one(int_stack); // now stack stores pointer to invalid memory for 7! } void add_one(stack *s) { int num = 7; stack_push(s, &num); } 24
![Generic stackpush void stackpushstack s const void data node newnode mallocsizeofnode newnodedata Generic stack_push void stack_push(stack *s, const void *data) { node *new_node = malloc(sizeof(node)); new_node->data](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-25.jpg)
Generic stack_push void stack_push(stack *s, const void *data) { node *new_node = malloc(sizeof(node)); new_node->data = malloc(s->elem_size_bytes); memcpy(new_node->data, s->elem_size_bytes); } new_node->next = s->top; s->top = new_node; s->nelems++; Solution 2: make a heap-allocated copy of the data that the node points to. 25
![intstackpopintstack s if snelems 0 error1 0 Cannot pop from empty int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-26.jpg)
int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty stack"); } int_node *n = s->top; int value = n->data; s->top = n->next; free(n); s->nelems--; } return value; 26
![Generic stackpop intstackpopintstack s if snelems 0 error1 0 Cannot pop Generic stack_pop int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-27.jpg)
Generic stack_pop int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty stack"); } int_node *n = s->top; int value = n->data; s->top = n->next; free(n); s->nelems--; } return value; Problem: we can no longer return the data itself, because it could be any size! 27
![Generic stackpop void intstackpopintstack s if snelems 0 error1 0 Cannot Generic stack_pop void *int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-28.jpg)
Generic stack_pop void *int_stack_pop(int_stack *s) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty stack"); } int_node *n = s->top; void *value = n->data; s->top = n->next; free(n); s->nelems--; } return value; While it’s possible to return the heap address of the element, this means the client would be responsible for freeing it. Ideally, the data structure should manage its own memory here. 28
![Generic stackpop void stackpopstack s void addr if snelems 0 error1 Generic stack_pop void stack_pop(stack *s, void *addr) { if (s->nelems == 0) { error(1,](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-29.jpg)
Generic stack_pop void stack_pop(stack *s, void *addr) { if (s->nelems == 0) { error(1, 0, "Cannot pop from empty stack"); } node *n = s->top; memcpy(addr, n->data, s->elem_size_bytes); s->top = n->next; } free(n->data); free(n); s->nelems--; Solution: have the caller pass a memory location as a parameter, and copy the data value to that location. 29
![Using Generic Stack intstack intstack intstackcreate for int i 0 i Using Generic Stack int_stack *intstack = int_stack_create(); for (int i = 0; i <](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-30.jpg)
Using Generic Stack int_stack *intstack = int_stack_create(); for (int i = 0; i < TEST_STACK_SIZE; i++) { int_stack_push(intstack, i); } We must now pass the address of an element to push onto the stack, rather than the element itself. 30
![Using Generic Stack stack intstack stackcreatesizeofint for int i 0 i Using Generic Stack stack *intstack = stack_create(sizeof(int)); for (int i = 0; i <](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-31.jpg)
Using Generic Stack stack *intstack = stack_create(sizeof(int)); for (int i = 0; i < TEST_STACK_SIZE; i++) { stack_push(intstack, &i); } We must now pass the address of an element to push onto the stack, rather than the element itself. 31
![Using Generic Stack intstack intstack intstackcreate intstackpushintstack 7 We must now pass the Using Generic Stack int_stack *intstack = int_stack_create(); int_stack_push(intstack, 7); We must now pass the](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-32.jpg)
Using Generic Stack int_stack *intstack = int_stack_create(); int_stack_push(intstack, 7); We must now pass the address of an element to push onto the stack, rather than the element itself. 32
![Using Generic Stack stack intstack stackcreatesizeofint int num 7 stackpushintstack num We Using Generic Stack stack *intstack = stack_create(sizeof(int)); int num = 7; stack_push(intstack, &num); We](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-33.jpg)
Using Generic Stack stack *intstack = stack_create(sizeof(int)); int num = 7; stack_push(intstack, &num); We must now pass the address of an element to push onto the stack, rather than the element itself. 33
![Using Generic Stack Pop off all elements while intstacknelems 0 printfdn Using Generic Stack // Pop off all elements while (intstack->nelems > 0) { printf("%dn",](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-34.jpg)
Using Generic Stack // Pop off all elements while (intstack->nelems > 0) { printf("%dn", int_stack_pop(intstack)); } We must now pass the address of where we would like to store the popped element, rather than getting it directly as a return value. 34
![Using Generic Stack Pop off all elements int poppedint while intstacknelems 0 Using Generic Stack // Pop off all elements int popped_int; while (intstack->nelems > 0)](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-35.jpg)
Using Generic Stack // Pop off all elements int popped_int; while (intstack->nelems > 0) { int_stack_pop(intstack, &popped_int); printf("%dn", popped_int); } We must now pass the address of where we would like to store the popped element, rather than getting it directly as a return value. 35
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-36.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers 36
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-37.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 4 2 12 -5 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 37
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-38.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 4 2 12 -5 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 38
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-39.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 12 -5 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 39
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-40.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 12 -5 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 40
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-41.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 12 -5 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 41
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-42.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 42
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-43.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 43
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-44.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 56 14 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 44
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-45.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 45
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-46.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 46
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-47.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 4 -5 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 47
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-48.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 -5 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 48
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-49.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 -5 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 49
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-50.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 -5 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 50
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-51.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 -5 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 51
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-52.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. 2 -5 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 52
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-53.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 53
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-54.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 54
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-55.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 55
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-56.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 56
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-57.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 57
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-58.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 58
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-59.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 59
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-60.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 60
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-61.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 61
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-62.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 62
![Bubble Sort Lets write a function to sort a list of integers Well Bubble Sort • Let’s write a function to sort a list of integers. We’ll](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-63.jpg)
Bubble Sort • Let’s write a function to sort a list of integers. We’ll use the bubble sort algorithm. -5 2 4 12 14 56 � • Bubble sort repeatedly goes through the array, swapping any pairs of elements that are out of order. When there are no more swaps needed, the array is sorted! 63
![Integer Bubble Sort void bubblesortintint arr int n while true bool swapped Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-64.jpg)
Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { if (arr[i-1] > arr[i]) { swapped = true; swap_int(&arr[i-1], &arr[i]); } } if (!swapped) { return; } How can we make this function generic, to sort an array of any type? 64
![Integer Bubble Sort void bubblesortintint arr int n while true bool swapped Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-65.jpg)
Integer Bubble Sort void bubble_sort_int(int *arr, int n) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { if (arr[i-1] > arr[i]) { swapped = true; swap_int(&arr[i-1], &arr[i]); } } if (!swapped) { return; } Let’s start by making the parameters and swap generic. 65
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-66.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { if (arr[i-1] > arr[i]) { swapped = true; swap_int(&arr[i-1], &arr[i]); } } if (!swapped) { return; } Let’s start by making the parameters and swap generic. 66
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-67.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { if (arr[i-1] > arr[i]) { swapped = true; swap(&arr[i-1], &arr[i], elem_size_bytes); } } if (!swapped) { return; } Let’s start by making the parameters and swap generic. 67
![Key Idea Generically Getting ith Elem A common generics idiom is getting a pointer Key Idea: Generically Getting i-th Elem A common generics idiom is getting a pointer](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-68.jpg)
Key Idea: Generically Getting i-th Elem A common generics idiom is getting a pointer to the i-th element of a generic array. From last lecture, we know how to get the last element generically: void swap_ends(void *arr, size_t nelems, size_t elem_bytes) { swap(arr, (char *)arr + (nelems – 1) * elem_bytes, elem_bytes); } We can generalize this to get the i-th element: void *ith_elem = (char *)arr + i * elem_size_bytes; 68
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-69.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (arr[i-1] > arr[i]) { swapped = true; swap(&arr[i-1], &arr[i], elem_size_bytes); } } if (!swapped) { return; } Let’s start by making the parameters and swap generic. 69
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-70.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (*prev_elem > *curr_elem) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { return; } Let’s start by making the parameters and swap generic. 70
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-71.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (*prev_elem > *curr_elem) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { return; } 71
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-72.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (*prev_elem > *curr_elem) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { return; } Wait a minute…this doesn’t work! We can’t dereference void *s OR compare any element with >, since they may not be numbers! �� 72
![A Generics Conundrum Weve hit a wall there is no way to A Generics Conundrum • We’ve hit a wall – there is no way to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-73.jpg)
A Generics Conundrum • We’ve hit a wall – there is no way to generically compare elements. They could be any type, and have complex ways to compare them. • How can we write code to compare any two elements of the same type? • That’s not something that bubble sort can ever know how to do. BUT – our caller should know how to do this, because they’re passing in the data…. let’s ask them! 73
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-74.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (*prev_elem > *curr_elem) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { Us: hey, you, person who called us. Do you know how return; to compare these two } elements? Can you help us? 74
![Generic Bubble Sort void bubblesortintvoid arr int n int elemsizebytes while true Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) {](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-75.jpg)
Generic Bubble Sort void bubble_sort_int(void *arr, int n, int elem_size_bytes) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (*prev_elem > *curr_elem) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { return; } Caller: yeah, I know how to compare those. You don’t know what data type they are, but I do. I have a function that can do the comparison for you and tell you the result. 75
![Generic Bubble Sort void bubblesortvoid arr int n int elemsizebytes function comparefn while Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, function compare_fn) { while](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-76.jpg)
Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, function compare_fn) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (compare_fn(prev_elem, curr_elem)) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } How can we compare these elements? They } if (!swapped) { return; } can pass us this function as a parameter. This function’s job is to tell us how to compare two elements of this type. 76
![Generic Bubble Sort void bubblesortvoid arr int n int elemsizebytes bool comparefnvoid a void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-77.jpg)
Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void *b)) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (compare_fn(prev_elem, curr_elem)) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } How can we compare these elements? They } if (!swapped) { return; } can pass us this function as a parameter. This function’s job is to tell us how to compare two elements of this type. 77
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-78.jpg)
Function Pointers A function pointer is the variable type for passing a function as a parameter. Here is how the parameter’s type and name are declared. bool (*compare_fn)(void *a, void *b) 78
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-79.jpg)
Function Pointers A function pointer is the variable type for passing a function as a parameter. Here is how the parameter’s type is declared. bool (*compare_fn)(void *a, void *b) Return type (bool) 79
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-80.jpg)
Function Pointers A function pointer is the variable type for passing a function as a parameter. Here is how the parameter’s type is declared. bool (*compare_fn)(void *a, void *b) Function pointer name (compare_fn) 80
![Function Pointers A function pointer is the variable type for passing a function as Function Pointers A function pointer is the variable type for passing a function as](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-81.jpg)
Function Pointers A function pointer is the variable type for passing a function as a parameter. Here is how the parameter’s type is declared. bool (*compare_fn)(void *a, void *b) Function parameters (two void *s) 81
![Generic Bubble Sort void bubblesortvoid arr int n int elemsizebytes bool comparefnvoid a void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-82.jpg)
Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, bool (*compare_fn)(void *a, void *b)) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (compare_fn(prev_elem, curr_elem)) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } } if (!swapped) { return; } 82
![Function Pointers bool integercomparevoid ptr 1 void ptr 2 int Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) {. . . } int](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-83.jpg)
Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) {. . . } int main(int argc, char *argv[]) { int nums[] = {4, 2, -5, 1, 12, 56}; int nums_count = sizeof(nums) / sizeof(nums[0]); bubble_sort(nums, nums_count, sizeof(nums[0]), integer_compare); return 0; } bubble_sort is generic, and works for any type. But the caller knows the specific type of data being sorted, and provides a comparison function specifically for that data type. 83
![Function Pointers Function pointers must always take pointers to the data they care Function Pointers • Function pointers must always take pointers to the data they care](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-84.jpg)
Function Pointers • Function pointers must always take pointers to the data they care about, since the data could be any size! When writing a callback, use the following pattern: 1) Cast the void *argument(s) and set typed pointers equal to them. 2) Dereference the typed pointer(s) to access the values. 3) Perform the necessary operation. (steps 1 and 2 can often be combined into a single step) 84
![Function Pointers bool integercomparevoid ptr 1 void ptr 2 cast arguments to Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-85.jpg)
Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int *s int *num 1 ptr = (int *)ptr 1; int *num 2 ptr = (int *)ptr 2; // dereference typed points to access values int num 1 = *num 1 ptr; int num 2 = *num 2 ptr; } // perform operation return num 1 > num 2; This function is created by the caller specifically to compare integers. 85
![Function Pointers bool integercomparevoid ptr 1 void ptr 2 cast arguments to Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-86.jpg)
Function Pointers bool integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int *s and dereference int num 1 = *(int *)ptr 1; int num 2 = *(int *)ptr 2; } // perform operation return num 1 > num 2; 86
![Comparison Functions Function pointers are used often in cases like this to compare Comparison Functions • Function pointers are used often in cases like this to compare](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-87.jpg)
Comparison Functions • Function pointers are used often in cases like this to compare two values of the same type. • The standard comparison function in many C functions provides even more information. It should return: • < 0 if first value should come before second value • > 0 if first value should come after second value • 0 if first value and second value are equivalent • This is the same return value format as strcmp! int (*compare_fn)(void *a, void *b) 87
![Function Pointers integercomparevoid ptr 1 void ptr 2 cast arguments to int Function Pointers integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-88.jpg)
Function Pointers integer_compare(void *ptr 1, void *ptr 2) { // cast arguments to int *s and dereference int num 1 = *(int *)ptr 1; int num 2 = *(int *)ptr 2; } // perform operation return num 1 – num 2; 88
![Generic Bubble Sort void bubblesortvoid arr int n int elemsizebytes int comparefnvoid a void Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, int (*compare_fn)(void *a, void](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-89.jpg)
Generic Bubble Sort void bubble_sort(void *arr, int n, int elem_size_bytes, int (*compare_fn)(void *a, void *b)) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (compare_fn(prev_elem, curr_elem) > 0) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } } if (!swapped) { return; } 89
![Tracing Bubble Sort int mainint argc char argv int nums 5 2 Tracing Bubble Sort int main(int argc, char *argv[]) { int nums[] = {5, 2};](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-90.jpg)
Tracing Bubble Sort int main(int argc, char *argv[]) { int nums[] = {5, 2}; bubble_sort(nums, 2, sizeof(int), int_cmp); return 0; } void bubble_sort(void *arr, int n, int elem_size_bytes, int (*compare_fn)(void *a, void *b)) { while (true) { bool swapped = false; for (int i = 1; i < n; i++) { void *prev_elem = (char *)arr + (i - 1) * elem_size_bytes; void *curr_elem = (char *)arr + i * elem_size_bytes; if (compare_fn(prev_elem, curr_elem) > 0) { swapped = true; swap(prev_elem, curr_elem, elem_size_bytes); } }. . . 90
![Comparison Functions Exercise how can we write a comparison function for bubble sort Comparison Functions • Exercise: how can we write a comparison function for bubble sort](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-91.jpg)
Comparison Functions • Exercise: how can we write a comparison function for bubble sort to sort strings in alphabetical order? • The common prototype provides even more information. It should return: • < 0 if first value should come before second value • > 0 if first value should come after second value • 0 if first value and second value are equivalent int (*compare_fn)(void *a, void *b) 91
![String Comparison Function int stringcomparevoid ptr 1 void ptr 2 cast arguments String Comparison Function int string_compare(void *ptr 1, void *ptr 2) { // cast arguments](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-92.jpg)
String Comparison Function int string_compare(void *ptr 1, void *ptr 2) { // cast arguments and dereference char *str 1 = *(char **)ptr 1; char *str 2 = *(char **)ptr 2; } // perform operation return strcmp(str 1, str 2); 92
![Function Pointer Pitfalls If a function takes a function pointer as a parameter Function Pointer Pitfalls • If a function takes a function pointer as a parameter,](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-93.jpg)
Function Pointer Pitfalls • If a function takes a function pointer as a parameter, it will accept it as long as it fits the specified signature. • This is dangerous! E. g. what happens if you pass in a string comparison function when sorting an integer array? 93
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-94.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers 94
![Function Pointers Function pointers can be used in a variety of ways For Function Pointers • Function pointers can be used in a variety of ways. For](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-95.jpg)
Function Pointers • Function pointers can be used in a variety of ways. For instance, you could have: • • A function to compare two elements of a given type A function to print out an element of a given type A function to free memory associated with a given type And more… 95
![Function Pointers Function pointers can be used in a variety of ways For Function Pointers • Function pointers can be used in a variety of ways. For](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-96.jpg)
Function Pointers • Function pointers can be used in a variety of ways. For instance, you could have: • • A function to compare two elements of a given type A function to print out an element of a given type A function to free memory associated with a given type And more… 96
![Common Utility Callback Functions Comparison function compares two elements of a given Common Utility Callback Functions • Comparison function – compares two elements of a given](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-97.jpg)
Common Utility Callback Functions • Comparison function – compares two elements of a given type. int (*cmp_fn)(const void *addr 1, const void *addr 2) • Cleanup function – cleans up heap memory associated with a given type. void (*cleanup_fn)(void *addr) • There are many more! You can specify any functions you would like passed in when writing your own generic functions. 97
![Generic Array Printing We would like to write a generic function that given an Generic Array Printing We would like to write a generic function that, given an](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-98.jpg)
Generic Array Printing We would like to write a generic function that, given an array, can print out each of its elements. • What parameters would this function need to take in? • How can we use function pointers to help us? 98
![Generics Overview We use void pointers and memory operations like memcpy and Generics Overview • We use void * pointers and memory operations like memcpy and](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-99.jpg)
Generics Overview • We use void * pointers and memory operations like memcpy and memmove to make data operations generic. • We use function pointers to make logic/functionality operations generic. 99
![Plan For Today Recap Generics with Void Finish up Generic Stack Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack](https://slidetodoc.com/presentation_image_h2/6fa7726eea7c5125fafd3aa79dfdb036/image-100.jpg)
Plan For Today • Recap: Generics with Void * • Finish up: Generic Stack • Function Pointers • Example: Bubble Sort • More Function Pointers Next time: Floats in C 100
Java generic array
Generics java erklärung
Benefits of generics in java
Java generics array
Using system.collections.generic
Hospitality generics
Kserializer
C generics
Function of inter
01:640:244 lecture notes - lecture 15: plat, idah, farad
Psalm 107 23-32
Dpr 80/2013 sintesi
107 prime?
Ang spratly island ay binubuo ng ilang pulo
Oklahoma south side locos
107 2010
Doc 107
Fas 115
Quanto devo aplicar hoje a taxa de 51 107 ao ano
Cs 107
Pl 107-208
Cbtis 107
Psalm 107:12
Tom sensky
Legge 107 del 2015
Mont 107
The verb estar (p. 107) answers
107 nott terrace schenectady ny
The verb estar page 107 answers
107 108 109
Bajkowe przedszkole 107
Legge n. 107 del 2015
101 102 103 104 105 106 107 108 109 110
Psalm 107:4-9
Art 107 cc
-2x^2-9=-107
Ppt 107
12*107
Ansi 107-2010
Gylot 107
Legge 13 luglio 2015 n. 107 sintesi
Radio 107,8
Machine uurtarief
Ac 20-107
107 1 fm
105 106 107
Caedb
Importance of pointers
C array of pointers to structs
Scale factor of pointer in c
Which is a good idea for using skip pointers?
Skip pointer
Which is a good idea for using skip pointers
Explicit pointer
Pointers and strings
Java pointers
Swizzling glsl
Shuffle left algorithm
Hazard pointers
12 pointers
I-need-a-few-pointers-98hpou5
Logbook examples for science project
Science fair logbook example
Pointers are variables that contain
What is the fundamental of pointers?
Pointers are variables that contain as their values
Converging pointers algorithm
Advanced pointers in c
9 pointers
Pointers are variables that contain as their values
Java pointers and references
Xkcd
Pointers symbol
Skip pointers in information retrieval
Converging pointers algorithm
Advanced pointers in c
Java pointers and references
File pointers
Pointers in assembly
Why do we study thermodynamics
Rational function parent function
Transformations of rational functions
Pressure is state function or path function
Domain of a constant function
Linear or non linear
Exponetial parent function
Pressure is state function or path function
Relation and function
Rational odd parent function
Absolute value of x as a piecewise function
How to identify a polynomial function
Forms of rational functions
How a predicate function become a propositional function?
Linear quadratic cubic
What is not a one to one function
Composite exponential function
Composite inverse
Rumus fungsi permintaan
Polynomial function parent function
Heat capacity is state or path function
Graphing polynomial functions