| | |
what is pointer to function ???
![]() |
•
•
•
•
Originally Posted by joshilay
can anyone tell me what are pointers to function ??? and how are they implemented ??
The adress of a function can be assimiled to its adress in memory, so the following code is valid :
C Syntax (Toggle Plain Text)
#include <stdio.h> void something() { printf("hello\n"); } void somethingElse() { printf("hello2\n"); } int main(void) { (void)(*pointer)() = something; pointer(); pointer = somethingElse; pointer(); return 0; }
Last edited by CrazyDieter; Jul 4th, 2006 at 3:27 pm.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Pointer to function are the pointers which have the ability to call the function pointed to by them.As the ordinary pointers take the address of variable and can change the value pointed by them, the function pointer almost does the same.
you can use typedef to clean declaration
typedef void (*pfct)(string);
void say(string);
void no(string);
void yes(string);
pfct my[] = { &say, &no, &yes };
also you can use the pointer to function in function's argument to specifify the details... like comparision criteria in sort funciton ;
typedef int (*Comfct)(MyType const*, MyType const*);
void sort(vector<MyType>& v,size_t n, Comfct Cmp);
Hope you got the basic idea..............
C Syntax (Toggle Plain Text)
#include <iostream> #include <string> using std::string; using std::cout; //simple function void print(string _name) { std::cout<<"Hello"<<_name; } //another function with return type int. int cal_pay(double _pay,int _hours) { return (2*_pay + 1000)*_hours; } //function pointer of the same type i.e. complete function type must match exactly void (*pfct)(string); //check the return type and arguments int (*cfct)(double,int); int main() { pfct=&print; //pfct points to print function pfct("Laiq"); cfct=cal_pay //also o.k. same as &cal_pay int ans = (*cfct)(500.00,4); //another way of calling return 0; }
you can use typedef to clean declaration
typedef void (*pfct)(string);
void say(string);
void no(string);
void yes(string);
pfct my[] = { &say, &no, &yes };
also you can use the pointer to function in function's argument to specifify the details... like comparision criteria in sort funciton ;
typedef int (*Comfct)(MyType const*, MyType const*);
void sort(vector<MyType>& v,size_t n, Comfct Cmp);
Hope you got the basic idea..............
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- c language problm, how to pass pointer to a function (C)
- pointer to function (C)
- Pointer to function as an argument (C++)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: Inventory administration program
- Next Thread: Graphics Help
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







. Thanks from my side