The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Function pointer
Discuss Function pointer in the C Programming forum on Dev Shed. Function pointer C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 9th, 2012, 12:37 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 30
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 1
|
|
|
Function pointer
Can anyone explain why do we need function pointers ? Where it is used ?
|

November 9th, 2012, 12:48 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Jump tables.
Passing to a function the function that it will need to call, selectable at run-time.
Passing to a CreateThread call the function that the thread will execute.
Just off the top of my head.
|

November 9th, 2012, 12:50 AM
|
 |
Contributed User
|
|
|
|
|

November 9th, 2012, 12:56 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
It is the address of a function through which the function can be called. Since it is a variable, the call can be dynamically determined at runtime rather than fixed when the code is built.
One use is in callbacks where you can for example pass a pointer to a function to some library code so that that library can call your code without prior knowledge of its existence. This is how the standard library qsort() function works for example to allow it to sort any type of object using user defined ordering rules.
There was once an excellent web site www.function-pointers.org which provided more that you would ever need to know about the subject, but it appears to be down or extinct. A reasonable summary of the subject can be found here
|

November 9th, 2012, 01:24 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by shilpac Can anyone explain why do we need function pointers ? Where it is used ? |
Function pointers add C++ like properties to C. You can create class like behaviour using structs and function pointers... Function pointers allows you to use functions like a variable and since functions have power to do something organization of complex programs can be more easier and flexible. You can create more generic behaviours and abstraction can be achieved. void pointers are also used frequently with function pointers.
|

November 9th, 2012, 01:52 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 30
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 1
|
|
|
Thanks guys.
|

November 9th, 2012, 06:01 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by serafon Function pointers add C++ like properties to C. |
That is a bit misleading - C has always supported function pointers, long before C++ even existed - nothing has been "added". In fact it is the other way around, C++ uses function pointers to enable some of its features such as virtual functions. But if that is what you are trying to achieve - just use C++!
|

November 9th, 2012, 07:52 PM
|
|
|
A function pointer can be used to make your code more "flexible" and ultimately easier to use.
Suppose you want a function called get_data which prompts the user for some number, and then validates that number based on some rule which is context-dependent. In this case, we can define some simple validator functions and then give the name of the function to our get_data function.
C Code:
Original
- C Code |
|
|
|
#include <stdio.h> #include <stdbool.h> // get_data(data, prompt, validator): Read data from user and use a custom validator. // The data is verified by calling the specified validator. If the validator returns false, // then the prompt is repeated until the user enters valid data. void get_data(double *data, const char *prompt, bool (*validator)(double)); // Validator functions: these should return true if the value is valid; false otherwise. bool validate_height(double height); bool validate_weight(double weight); bool validate_any(double any) {return true;} // always valid // main: demonstrate use of get_data() with function pointers int main() { double height, weight, random; get_data(&height, "Enter your height in cm: ", validate_height); get_data(&weight, "Enter your weight in kg: ", validate_weight); get_data(&random, "Enter a random number: ", validate_any); printf("\nThanks, here is your info summary:\n" "Height: %.2f cm\n" "Weight: %.2f kg\n" "Random: %g\n", height, weight, random); return 0; } bool validate_height(double height) { if (height < 5.0 || height > 300.0) return false; return true; } bool validate_weight(double weight) { if (weight < 10.0 || weight > 500.0) return false; return true; } void get_data(double *data, const char *prompt, bool (*validator)(double)) { for (;;) { if (scanf("%lf", data) != 1) { fflush(stdin); // skip over non-numeric data printf("A number is required.\n"); continue; } getchar(); // skip whitespace if ((*validator)(*data)) return; printf("Sorry, that was not a valid number.\n"); } }
Using such a scheme is nice, because if we add more rules later, we don't need to change get_data to handle them. We just define another function and pass it along. The get_data function does "the right thing" as long as the validator is properly defined.
|

November 9th, 2012, 08:57 PM
|
|
Contributing User
|
|
Join Date: Apr 2012
Posts: 58
Time spent in forums: 1 Day 17 h 52 m 2 sec
Reputation Power: 2
|
|
|
What if the function takes arguments? Then is it still possible to select the function by dereferencing a pointer to the function that changes at run time? I think not.
|

November 9th, 2012, 10:43 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by EEmaestro What if the function takes arguments? Then is it still possible to select the function by dereferencing a pointer to the function that changes at run time? I think not. |
Function already has parameters if you look carefully. There is no difference from an ordinary function, it has all the capabilities.
One problem here is, (actually not a problem but getting more flexibility) using functions with different number of arguments. Because there is no restriction about arguments of the function that the pointer points to but the number and type should be always same.
One solution is to use va_arg, va_list etc (i also asked in another topic) and second may be an argument which is a void pointer and points to a structure of arguments.
|

November 10th, 2012, 03:05 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by EEmaestro What if the function takes arguments? Then is it still possible to select the function by dereferencing a pointer to the function that changes at run time? I think not. |
That can be done without any problem; did you look at any of the links already posted? The function passed to qsort() for example takes two arguments.
For example:
Code:
double(*trig_fn)(double);
trig_fn = &sin ;
double x = trig_fn( theta ) ;
Note that explicit de-referencing is not required, though allowed.
In the function pointer declaration, the return value and the parameter types form part of the function-pointer type. At compile time at least, the data type is checked for consistency. You can also have a generic function pointer type and cast it to another in order to force a call for which the parameter types are not a match - obviously you'd do that with care, but it is sometimes useful - for example you might have a call back that does not need any parameters being called from a library that provides parameters. In this case you could have a dummy parameter and ignore it or cast the pointer to the accepted type.
In C the stack frame setup-up and tear down code is performed by the caller not the called function, so nothing bad happens in terms of stack management, but interpreting arguments that were not explicitly passed will only yield junk, modifying such parameters or dereferencing pointer arguments will likely cause memory corruption and even more trouble.
|

November 10th, 2012, 03:15 AM
|
|
|
Quote: | Originally Posted by clifford Note that explicit de-referencing is not required, though allowed.
|
Indeed. The above example can also be written (more clearly in my opinion):
C Code:
Original
- C Code |
|
|
|
double (*trig_fn)(double); trig_fn = sin ; double x = (*trig_fn)(theta);
This is the style used by the K&R book to explain function pointers. The unadorned sin should already be an address, and the identifier (*trig_fn)(...) has the type `double', wheras the unadorned trig_fn has type `double (*)(double)'
|

November 10th, 2012, 09:15 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by clifford
In C the stack frame setup-up and tear down code is performed by the caller not the called function, so nothing bad happens in terms of stack management, but interpreting arguments that were not explicitly passed will only yield junk, modifying such parameters or dereferencing pointer arguments will likely cause memory corruption and even more trouble. |
Can you explain this more please? Do you say it is not a good idea to pass in a void pointer to a function that points to a parameter list and dereference it in the called function?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|