lecture25-mar29

# lecture25-mar29 - Announcements - Lecture 25 Assignment 5...

This preview shows pages 1–2. Sign up to view the full content.

1 Announcements - Lecture 25 • Assignment 5 – program due Thursday Corrections to function headers double median(struct node *head) finds the median of a given data set (data is in a linked list format) struct node *sortDown(struct node *list) sorts a given linked list into descending order and returns a pointer to the resultant list struct node *merge (struct node *head1, struct node *head2) merges two sorted linked lists (without destroying either of them) and returns a pointer to the resultant list • Exam 2 – next Monday, April 5 th – Sample exam posted on BB this afternoon • Topics for today Function pointers Advanced declarations (Ch. 18 – not the C99 stuff) – Variables – Functions Function Pointers You can also use a pointer to point to the entry point of a function - (*f) is the notation – call that function through the pointer, rather than by it’s function name Example: declare and use a function pointer variable char (*pf) (int); /* function header */ means that pf can hold a pointer to any function that takes an int argument and returns a char pf = realFcnName; /* char realFcnName (int x); */ putchar (pf(i)); /* or putchar ((*pf) (i)); */ You can also use that pointer to pass the function as an argument to another function Pointers to functions can be used in numerous other ways – A function can return a pointer to another function. – An array may contain a series of pointers to functions. Functions Passed as Arguments int findZero (int (*f)(int)); int poly1(int); int poly2(int); int main(void) { int k; k = findZero( poly1 )); printf("Answer: %d\n",k); return 0; } int findZero(int (*f)(int)) { /* find where f(n) = 0 for a given polynomial fcn */ int n = 0; while ( (*f)(n) != 0 ) {n++;} return n; } int poly1(int i) { return i * i + i - 12; } int poly2 (int i) { return i * i + 2 * i - 73 ; } We want to write a program to find a root for y = f(x) = 0 Where f(x) is a polynomial defined by a given function k = findZero( poly2 )); The qsort Function Certain functions in the C standard library require a function pointer as a parameter. One of the most commonly used is qsort, a general-purpose algorithm capable of sorting any array. The following declaration for qsort appears in <stdlib.h>: void qsort (void *base, size_t num, size_t size, int (*compar) (const void *p, const void *q)); • base is a pointer to the first element in the array. • num is the number of elements in the array. • size is the size of each array element. compar is a pointer to a function that compares two array elements pointed to by parameters p and q. By convention, when given two pointers p and q to array

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 08/30/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

### Page1 / 3

lecture25-mar29 - Announcements - Lecture 25 Assignment 5...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online