{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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 elements, compar returns a number that is Negative if *p is “less than” *q.
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern