Function Pointers are pointers, i.e. variables, which point to the address of a function. Thus they exhibit polymorphism by call by different methods. Function pointers is an exclusive propery of the c language and should be used wisely as functions are always more powerful and fact in execution as compared to function pointers.
Consider the following example.
The function pointer *funcpoint is declared here as void (*funcpoint)(int), which specifies both the return type (void) and the types of arguments (int) of the function. We then assign the pointer to a particular function. Now we can call the function normally. This gives a double nature to teh function.Code:#include<stdio.h> #include<conio.h> void new_(int); void old(int); void main(void) { void (*funcpoint)(int); int number; clrscr(); printf("Enter a number"); scanf("%d", &number); funcpoint = (number > 5) ? old : new_; funcpoint(number); getch(); } void new_(int n) { printf("The number %d, is less or equal to 5.\n", n); } void old(int m) { printf("The number %d, is more than 5.\n", m); }
God is real... unless declared an integermy blog :: http://techarraz.com/
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks