473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function Pointer.

Hi,
please can you tell me use of function pointer and its advantage.
Thanks,
Akhil
May 24 '07 #1
6 1729
Savage
1,764 Expert 1GB
Hi,
please can you tell me use of function pointer and its advantage.
Thanks,
Akhil
function pointer is,as it's name tells, a pointer to a function.By using file pointers u can make ur code shorter and more readable.

Nice example of function pointer is a function pointer which is used for navigating trough some type of menu.

e.g

void (*pfunc) (int *x,int *y);//<-Function pointer

void incy(int *x,int *y);//Function whcih increase y
void decy(int *x,int *y);// decreases y
void incx(int *x,int *y);
void decx(int *x,int *y);

//Code to determine which key is pressed

........
e.g

case UPKEY: pfunc=incy;

and so on.


Also f.ptrs are not declared,they "steal" declarations from function which they point to.

Savage
May 24 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Hi,
please can you tell me use of function pointer and its advantage.
Thanks,
Akhil
A function pointer is used to allow you to make a run-time decision on what function to call. Without this feature all of your function calls must be known at compile time. That makes this problem much harder to solve:

"Write a function to sort an array of int in a sequence determined by the user from a menu choice".

At the time you write the sort function you have no idea what the sequence is. So, you write the sort function by asking for the address of the compare function to be used by the sort. That is a function pointer.
May 24 '07 #3
Thank you for your Reply could you please give me a example step by step.
Thanks $ Regards,
Akhil
May 25 '07 #4
Thank you for your Reply could you please give me a example step by step.
Thanks $ Regards,
Akhil
Hi,

Here is a site that explains function pointers for both in C and C++, which I found very helpful:
http://www.newty.de/fpt/index.html

Thomas
May 25 '07 #5
sowmi
3
Even I have a doubt in the same,
whether is it possible to have the function pointers for the functions where the return type and arguements are different???

Eg :
int aaa(int a,int b);
void bbb(void);
int ccc(char *aa,char *bb,char *cc)

Thanks,
Sowmi
Jun 24 '07 #6
kky2k
34
Even I have a doubt in the same,
whether is it possible to have the function pointers for the functions where the return type and arguements are different???

Eg :
int aaa(int a,int b);
void bbb(void);
int ccc(char *aa,char *bb,char *cc)

Thanks,
Sowmi
No its not possible the fn ptr declarations of above will be unique so the declaration will be,

int aaa(int a,int b); // int (*ptr) (int,int)
void bbb(void); // Void(*ptr)(void)
int ccc(char *aa,char *bb,char *cc) // int (*ptr)(char*,char*,char*)

also keep note of (*ptr)..its different than (ptr*)..its a common mistake people use to do while declaring fn pointer
Jun 24 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.