Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.

Similar presentations


Presentation on theme: "Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable."— Presentation transcript:

1 Pointers to Functions In C programming language

2 Introduction  While many programming languages support the concept of pointers to data, only a few enable you to define pointers to code -- that is, pointers that point to functions.  Originally introduced in C, pointers to functions are widely used in C++  Unfortunately, their cumbersome syntax baffles both novices and experienced programmers.

3 What are function Pointers?  C does not require that pointers only point to data, it is possible to have pointers to functions  Functions occupy memory locations therefore every function has an address just like each variable

4 Why do we need function Pointers?  Useful when alternative functions maybe used to perform similar tasks on data (eg sorting)  One common use is in passing a function as a parameter in a function call.  Can pass the data and the function to be used to some control function  Greater flexibility and better code reuse

5 Define a Function Pointer  A function pointer is nothing else than a variable, it must be defined as usual. Eg, int (*funcPointer) (int, char, int); funcPointer is a pointer to a function.  The extra parentheses around (*funcPointer) is needed because there are precedence relationships in declaration just as there are in expressions

6 Assign an address to a Function Pointer  It is optional to use the address operator & infront of the function’s name  When you mention the name of a function but are not calling it, there’s nothing else you could possibly be trying to do except for generating a pointer to it  Similar to the fact that a pointer to the first element of an array is generated automatically when an array appears in an expression

7 Assign an address to a Function Pointer //assign an address to the function pointer //assign an address to the function pointer int (*funcPointer) (int, char, int); int firstExample ( int a, char b, int c){ printf(“ Welcome to the first example”); printf(“ Welcome to the first example”); return a+b+c; return a+b+c;} funcPointer= firstExample; //assignment funcPointer=&firstExample; //alternative using address operator

8 Comparing Function Pointers  Can use the (==) operator //comparing function pointers //comparing function pointers If (funcPointer == &firstExample) printf (“pointer points to firstExample”);

9 Calling a function using a Function Pointer  There are two alternatives 1) Use the name of the function pointer 2) Can explicitly dereference it int (*funcPointer) (int, char, int); // calling a function using function pointer int answer= funcPointer (7, ’A’, 2 ); int answer= funcPointer (7, ’A’, 2 ); int answer=(* funcPointer) (7, ’A’, 2 ); int answer=(* funcPointer) (7, ’A’, 2 );

10 Arrays of Function Pointers  C treats pointers to functions just like pointers to data therefore we can have arrays of pointers to functions  This offers the possibility to select a function using an index Eg. suppose that we’re writing a program that displays a menu of commands for the user to choose from. We can write functions that implement these commands, then store pointers to the functions in an array: suppose that we’re writing a program that displays a menu of commands for the user to choose from. We can write functions that implement these commands, then store pointers to the functions in an array:

11 void (*file_cmd[]) (void) = void (*file_cmd[]) (void) = { new_cmd, open_cmd,close_cmd, save_cmd, save_as_cmd, print_cmd, print_cmd, exit_cmd exit_cmd}; If the user selects a command between 0 and 6, then we can subscript the file_cmd array to find out which function to call file_cmd[n]();

12 Trigonometric Functions // prints tables showing the values of cos,sin #include #include void tabulate(double (*f)(double), double first, double last, double incr); main(){ double final, increment, initial; printf (“Enter initial value: “); scanf (“%lf”, &initial); printf (“Enter final value: “); scanf (%lf”, &final); printf (“Enter increment : “); scanf (%lf”, &increment); Printf(“\n x cos(x) \n” “ ---------- -----------\n”); “ ---------- -----------\n”); tabulate(cos, initial,final,increment); Printf(“\n x sin (x) \n” “ ---------- -----------\n”); tabulate(sin, initial,final,increment); return 0; }

13 Trigonometric Functions // when passed a pointer f prints a table showing the value of f void tabulate(double (*f) (double), double first, double last, double incr) { double x; int i, num_intervals; num_intervals = ceil ( (last -first) /incr ); for (i=0; i<=num_intervals; i++){ x= first +i * incr; printf(“%10.5f %10.5f\n”, x, (*f) (x)); }}

14 Enter initial value: 0 Enter final value:.5 Enter increment:.1 Xcos(x) ----------------------- 0.000001.00000 0.100000.99500 0.200000.98007 0.300000.95534 0.400000.92106 0.500000.87758 Xsin(x) ----------------------- 0.000000.00000 0.100000.09983 0.200000.19867 0.300000.29552 0.400000.38942 0.500000.47943

15 Sorting  Consists of three parts 1) a comparison that determines the ordering of any pair of objects 2) an exchange that reverses their order 3) A sorting algorithm that makes comparisons and exchange until the objects are in order.  qsort will sort an array of elements. This is a wild function that uses a pointer to another function that performs the required comparisons.  Library: stdlib.h Prototype: void qsort ( void *base, size_t num, size_t size, int (*comp_func) (const void *, const void *))  Some explanation.  void * base is a pointer to the array to be sorted. This can be a pointer to any data type  size_t num The number of elements.  size_t size The element size.  int (*comp_func)(const void *, const void *))This is a pointer to a function.

16 Sorting  qsort thus maintains it's data type independence by giving the comparison responsibility to the user.  The compare function must return integer values according to the comparison result: less than zero : if first value is less than the second value less than zero : if first value is less than the second value zero : if first value is equal to the second value zero : if first value is equal to the second value greater than zero : if first value is greater than the second value greater than zero : if first value is greater than the second value  Some quite complicated data structures can be sorted in this manner.  The generic pointer type void * is used for the pointer arguments, any pointer can be cast to void * and back again without loss of information.

17 qsort #include #include #define NSTRS 10 /* number of strings */ #define STRLEN 16 /* length of each string */ char strs [NSTRS] [STRLEN]; /* array of strings */ main() { int i; extern int compare1(), compare2(); /* Prompt the user for NSTRS strings. */ for (i = 0; i < NSTRS; i++) { printf ("Enter string #%d: ", i); gets(strs[i]);}

18 qsort /* * Sort the strings into ascending order. There are NSTRS array elements, each one is STRLEN characters long.*/ qsort(strs, NSTRS, STRLEN, compare1); /* * Print the strings. */ printf("\nSorted in ascending order:\n"); for (i = 0; i < NSTRS; i++) printf( "\t%s\n", strs[i] ); /* * Now sort the strings in descending order. */ /* * Now sort the strings in descending order. */ qsort(strs, NSTRS, STRLEN, compare2);

19 qsort printf("\nSorted in descending order:\n"); for (i = 0; i < NSTRS; i++) printf("\t%s\n", strs[i]); exit(0); }//end main /* * compare1--compare a and b, and return less than greater than, or equal to zero. Since we are comparing character strings, we can just use strcmp to do the work for us. */ can just use strcmp to do the work for us. */ compare1(a, b) char *a, *b; { return(strcmp(a, b)); } /* * compare2--this compares a and b, but is used for * sorting in the opposite order. Thus it * sorting in the opposite order. Thus it returns the opposite of strcmp. We can * simulate this by simply reversing the * arguments when we call strcmp. */ compare2(a, b) char *a, *b; { return(strcmp(b, a)); return(strcmp(b, a)); }

20 Function Pointers and Ordinary Pointers  C makes a clear distinction between the two types of pointers  A data pointer only hold the address of the first byte of a variable  While function pointers share many similarities with ordinary data pointers, they differ in several ways. First, you can't declare a generic pointer to function similar to void*. In addition, trying to store a function pointer in a void* isn't guaranteed to work (certain compilers tolerate this, others don't). Finally, you can't dereference a function pointer -- there's no such thing as passing a function by value.

21 References  “The C Programming Language”, Brian W.Kernighan, Dennis M.Ritchie  http://www.codetoad.com/c_pointers.asp http://www.codetoad.com/c_pointers.asp  http://www.eskimo.com/~scs/cclass/int/sx1 0.html http://www.eskimo.com/~scs/cclass/int/sx1 0.html http://www.eskimo.com/~scs/cclass/int/sx1 0.html

22 Thaaaaaaaaaaaaaanks!


Download ppt "Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable."

Similar presentations


Ads by Google