•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,171 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,168 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1314 | Replies: 5
![]() |
•
•
•
•
Originally Posted by joshilay
can anyone tell me what are pointers to function ??? and how are they implemented ??
in C, functions are defined just like variable.
The adress of a function can be assimiled to its adress in memory, so the following code is valid :
#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 2:27 pm.
•
•
Join Date: Jun 2006
Posts: 81
Reputation:
Rep Power: 3
Solved Threads: 8
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..............
#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..............
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- pointer to function (C)
- Pointer to function as an argument (C++)
- c language problm, how to pass pointer to a function (C)
- Assign content to a function pointer (C)
Other Threads in the C Forum
- Previous Thread: Inventory administration program
- Next Thread: Graphics Help



. Thanks from my side
Linear Mode