Function Pointers and Abstract Data Types
Function Pointers and Abstract Data Types
COS 217
1
Goals of Today’s Lecture
• Function pointers
o Sorting an array of integers
o Sorting an array of strings
o Sorting an array of any type
– Void pointers and casting
– Pointers to functions
2
Sorting an Array of Integers
• Example problem
o Input: array v of n integers
o Output: array in sorted order, from smallest to largest
• Simple approach
o Go one by one through the n array elements
o By the end of step i, get ith smallest value in element i
– Compare element i with all elements after it
– Swap values if the ith element is larger
3
Integer Sorting Example
v[0] > v[1]? 7 2 9 6 v[1] > v[2]? 2 7 9 6
4
Integer Sorting Function
5
Sorting an Array of Strings
• Data types are different
o Array elements are char*
o Swap variable is char*
0 “the” 0 “brown”
1 “quick” 1 “fox”
2 “brown” 2 “quick”
3 “fox” 3 “the”
6
String Sorting Function
7
Creating a Generic Function
• Generic function
o A single sort() function that works for all data types
8
Generalizing: Void Pointers
• Generic pointers are the same as any other pointer
o Except they point to a variable with no specific type
o Example: void *datap = “CS217”;
Memory
• Difference: 0x00000000
• Common Uses:
o Abstract data types 0x10005384
supporting polymorphism* datap
o Pass pointer to function that
could be any of several types 0xFFFFFFFF
10
Casting: Explicit Type Conversions
• Casting
o As if the expression were assigned to a variable of the specified type
o E.g., int *intp1 cast into void pointer by (void *) intp1
int main() {
char* w[4] = {“the”, “quick”, “brown”, “fox”};
pointer to a function
13
Using Generic Sort With Integers
#include <stdio.h>
#include “sort.h”
int main() {
int* w[4];
pointer to a function
w[0] = malloc(sizeof(int));
*(w[0]) = 7;
…
sort((void **) w, 4, (int (*)(void*,void*))CompareInts);
…
}
14
Making “Array” an ADT
• Arrays in C are error prone
o Access elements before the array starts (e.g., v[-1])
o Access elements past the end of array (e.g., v[n])
o Modify the variable that keeps track of size (e.g., n)
15
Array ADT: Interface
array.h client does not know implementation
16
Client Using Array ADT: Strings
#include “array.h”
#include <stdio.h>
int main() {
Array_T array;
int i;
array = Array_new();
Array_free(array);
return 0;
}
17
Client Using Array ADT: Integers
#include “array.h”
#include <stdio.h>
int main() {
Array_T array;
int one=1, two=2, three=3;
int i;
array = Array_new();
Array_free(array);
return 0;
} 18
Array ADT Implementation
#include “array.h”
struct Array {
void *elements[MAX_ELEMENTS];
int num_elements;
};
Array_T Array_new(void) {
Array_T array = malloc(sizeof(struct Array));
array->num_elements = 0;
return array;
}
20
Array ADT Implementation (Cont.)
void Array_insert(Array_T array, int index, void *datap) {
int i;
array->num_elements--;
} 21
Array ADT Implementation (Cont.)
22
Stupid Programmer Tricks
• qsort takes int (*compar)(const void *, const void *)
o Comparison function returns integer greater than, equal, less than
zero if first argument is greater than, equal, less than second
• Common approach:
int
ItemCompare(const void *pA, const void *pB)
{
Item *a = pA, *b = pB;
return(a->field - b->field);
}
23
Summary
• Module supporting operations on single data structure
o Interface declares operations, not data structure
o Interface provides access to simple, complete set of operations
o Interface provides flexibility and extensibility
• Advantages
o Provide complete set of commonly used functions (re-use)
o Implementation is hidden from client (encapsulation)
o Can use for multiple types (polymorphism)
24