Reputation: 13
What is the utility of function pointers? For example, if I have this:
#include <stdio.h>
void Print1(int(*number)(void)){
printf("%d", number());
}
void Print2(int a)
{
printf("%d", a);
}
int con(void)
{
return 2;
}
int main(void)
{
Print1(&con);
Print2(con());
}
Both give the same result, one with pointer to functions other with calling one function to another, conclusion? I can't see the difference.
Upvotes: 1
Views: 131
Reputation: 23236
Others have commented on the specifics of your example code already, so I will try to address your question: What is the utility of function pointers? in a general way:
Function pointers are especially useful in C * (see * at bottom) when you have a collection of functions that are similar in that they contain the same argument list, but have different outputs. You can define an array of function pointers, making it easier, for example, to call the functions in that family from a switch, or a loop, or when creating a series of threads in a pool that include similar worker functions as arguments. Calling an array makes it as simple as changing the index of the array to get the specific functionality you need for each unique case.
As a simple example, the two string functions strcat()
and strcpy()
have the argument list: (char *, const char *)
, therefore, may be assigned to an array of function pointers. First create the function pointer array:
char * (*pStr[2])( char *a, const char *b);` //array of function pointers pStr[]
Then, make the assignements of strcat and strcpy to the array:
void someFunc(void)
{
pStr[0] = strcat; //assign strcat to pointer [0]
pStr[1] = strcpy; //assign strcpy to pointer [1]
}
Now, strcat()
or strcpy()
can be called as:
int main(void)
{
char a[100]="kjdhlfjgls";
char b[100]="kjdhlfjgls";
someFunc();//define function pointers
pStr[0](a, "aaaaaaaa"); //strcat
pStr[1](b, "aaaaaaaa"); //strcpy
return 0;
}
Example output:
This is just a simple example. It does not explore the full extent of usefulness function pointers can provide, but illustrates another reason why function pointers may be preferred in some situations.
* This illustration is targeted only to C, as opposed to C++, where qualities of inheritance and polymorphism inherent to that language would make this suggestion unnecessary.
Upvotes: 0
Reputation: 385
Look at the function qsort in stdlib.h
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
It sorts the array base, size nmemb of objects of size size with the quicksort algorithm - but with a compare function YOU provide. So if you want to sort some array with weird data types in it, you just have to write your compare function, and NOT the entire quicksort algorithm.
Obviously windows also uses callback functions in its message loop, and I think people can find many examples showing its usefulness (and frankly i feel in some situations it is almost required)
Upvotes: 2
Reputation: 35788
In your first example:
Print1(&con);
You are passing the function itself to Print1
. In your second example:
Print2(con());
You are calling con
when you call Print2
, passing that value to Print2
. This is important if con
has side effects. Imagine a method that does something in the background (say a disk read) that takes a long time. If you build it to take a function pointer to a completion function, that function will be called when the task is done. If you don't, you are just calling your completion function before you have even started the function.
Imagine if you had a function defined as so:
void LongTask(void(*completed)()) {
sleep(5);
completed();
}
It has to be defined to take a function pointer or the callback will be called before the function even starts.
Upvotes: 2
Reputation: 65
Print2(con());
is just the return value of that function, not a pointer to the function.
Upvotes: 0