lecture19-preproc

lecture19-preproc - Announcements Assignment 5 Assignment 5...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
nnouncements Announcements ssignment 5 Assignment 5 Due Thursday pics for Today Topics for Today Function Pointers Preprocessor
Background image of page 1

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

View Full DocumentRight Arrow Icon
nction Pointer Function Pointer function pointer points to the entry point of a function A function pointer points to the entry point of a function (notation: (*f), where f is the function name) Allows you to call the function through the pointer instead of the name You can declare and use function pointer variables char (*pfunct) (int); /* pfunct can store a pointer to a function that takes an int argument and returns a char */ f t lF ti N 1 pfunct = realFunctionName1; putchar (pfunct (i)); /* or putchar((*pfunct) (i));*/
Background image of page 2
nction Pointers (2) Function Pointers (2) u can also use a function pointer to pass the You can also use a function 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. A variable can store a pointer to a function. An array may contain a series of pointers to functions.
Background image of page 3

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

View Full DocumentRight Arrow Icon
nction Pointers: Example Function Pointers: Example Write a program that finds the root of a function f(x), where f(x) is a polynomial defined by a given function int find_zero(int (*f)(int)); int poly(int i); int main(void) { printf(“Answer: %d\n”, find_zero(poly)); return 0; } t fi d (i t (*f)(i t)) { int find_zero(int (*f)(int)) { int n = 0; while ((*f)(n)) n++; /* or while (f(n)) n++; */ eturn n; return n; } int poly(int i) { eturn i* i+i 2; return ii + i 12; }
Background image of page 4
he sort nction The qsort Function me functions in the C standard library require a Some functions in the C standard library require a function pointer as a parameter qsort is a general purpose algorithm for sorting any array: void qsort(void *base, size_t nmemb, size_t size, t(* )( t id* t id*) int (*compar)(const void *, const void *); base is a pointer to the first element nmemb 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
REPROCESSOR (CHAPTER 14) PREPROCESSOR (CHAPTER 14)
Background image of page 6
reprocessor Directives Preprocessor Directives reprocessing: making changes to the xt f Preprocessing: making changes to the text of the source program one before actual compilation begins (or appears Done before actual compilation begins (or appears to be) reprocessor doesn’t know C Preprocessor doesn t know C Preprocessor directives df ii t i Macro definition Conditional compilation File inclusion
Background image of page 7

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

View Full DocumentRight Arrow Icon
Program Production Process library functions IDE nker C library functions (.obj) C Compiler Linker any C object code Executable achine code C source file (.c) many (.obj) Machine code
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/11/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

Page1 / 32

lecture19-preproc - Announcements Assignment 5 Assignment 5...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online