Advanced Pointers
Advanced Pointers
Pointers to functions
Declaration:
pointer to a function that takes an integer argument and a float argument and returns an integer
returnType (*varName)(parameterTypes);
Examples:
pointer to a function that takes an integer argument and a float argument and returns a pointer to an integer An array of pointers to functions Each function takes an integer argument and a float argument and returns a pointer to an integer
poly (many) + morph (shape) A polymorphic language can handle a range of different data types (shapes?) with a single statement
INTNODE *search_list(INTNODE *node, int const key) { while (!node) { if (node->value == key) break; node = node->next; } return node; }
void construct_node(NODE *node, void *value, NODE *next) { node->value = value; node->next = next; } NODE *new_node(void *value, NODE *next) { NODE *node = (NODE *)malloc(sizeof(NODE)); construct_node(node, value, next); return node; }
5 CS 3090: Safety Critical Programming in C
What is it that makes the old search_list only work for integers?
The key parameter is of type int The == operator is used to compare int values but == will not work for many types (e.g. structs, strings)
Programmer must supply a comparison function thats appropriate for the data type being stored in the nodes This function argument is called a callback function:
Caller passes in a pointer to a function Callee then calls back to the caller-supplied function
If our nodes hold strings, we have a compare function already defined: strcmp or strncmpy
& is optional here
compiler will implicitly take the address #include <string.h> match = search_list(root, "key", &strcmp);
Note: you may get a warning, since strcmp is not strictly of the right type: its parameters are of type char * rather than void *
If our nodes hold other kinds of data, we may need to roll our own compare function
int compare_ints(void const *a, void const *b) { const int ia = *(int *)a, ib = *(int *)b; return ia != ib; } match = search_list(root, key, &compare_ints);
Jump tables
In some cases, a nice alternative to long, repetitive switch statements, like this:
add(double, sub(double, mul(double, div(double, double); double); double); double);
switch(oper) { case ADD: result case SUB: result case MUL: result case DIV: result }
10
= = = =
Jump tables
Array of pointers to functions. Each function takes two doubles and returns a double
double (*oper_func[])(double, double) = { add, sub, mul, div }; result = oper_func[oper](op1, op2);
11 CS 3090: Safety Critical Programming in C
Safest outcome: memory error, and program is terminated But what if the garbage value is a valid address?
Worst case: address contains program instruction execution continues, with random results Hard to trace the cause of the erroneous behavior
12
C programs can be called from the command line, with certain arguments entered along with the program name: e.g. Registration program register You may register with an existing ID by the i option: register -i ID Otherwise, an ID will be generated
13
main
holds the number of arguments argv is an array of strings: the nth command line string is stored at argv[n-1]
argc
register -i wallace
3 argc
NUL
14
References
16