What is the difference between Function Pointer vs Function Call?

Hello friends,
How can I use an array of function pointers? If we see the link above, it will tell us how the function pointer works.
Now the question is, why should I choose a function pointer?
Why can't I use function call directly? What benefits will I get with a function pointer?
eg

enum{a, b, c}; void A(); void B(); void C(); void (*fun[3])() = {A, B, C}; Now when I need to call a function I am doing like, fun[a](); // This calls function A // And so on... 

You can do the same in function calls.

for example, when I need to call function A, B or C. I can just like

 A(); B(); or C(); 

Then why a function pointer ?

+6
source share
7 answers

Now the question is, why should I choose a function pointer?
What benefits will I get with a function pointer?

You use function pointers when you need to implement an asynchronous mechanism.
You need the function to be called asynchronously when something happens. How do you know which function to call?
The address of each function is unique, so you need to use and store the address of the function.
Where do you store this function address? Function pointer

+5
source

There are many reasons to use a function pointer, in particular, to perform common actions in C.

The main place you will see is used as an argument to the function. For example, using the bsearch function bsearch it uses the comparison function passed as a function pointer to compare elements and sort data:

 void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); 

This allows bsearch be generic and sort data of any type, since only this comparison function needs to know the data type.


Another way is to avoid multiple checks. i.e.

 void makePizza(void) { } void bakeCake(void) { } iWantPizza = 1; ... if (iWantPizza) makePizza(); else bakeCake(); /* want to call the same function again... */ if (iWantPizza) makePizza(); else bakeCake(); .. /* alternative */ void (*makeFood)(void) = iWantPizza ? makePizza : bakeCake; makeFood(); /* want to call the same function again... */ makeFood(); 
+11
source

In the example you showed, one more thing can be done.

Suppose that there is a function that must be run for some operation with the device. In a simple way, you can write all function calls to another main function and call this master function.

Another way to do this is to write all the function names in curly braces and call them using the function pointer and loop. It looks smart. I'm not sure how this will help you better, but I saw this in the Linux kernel code.


I agree with all the answers here. Other than that, I have some of my own judgments for using a function pointer.

Let's look at an example of complex mathematical calculation (for example, Fibonacci printing, integration, Fourier Xform, etc.).

You have an FX function (which does this complex mathematical calculation or something else) that you use many times in your program. This function is used in many different tasks.

After using your program for several months, you will find out that for some work you can improve the function, and for some - the best. What will you do? Write a new function, go and change the name of the function in all places.

Every time you find something better, you will do the same.

Instead, use a different function pointer for another job. At the initial stage, all pointers can point to one function. When you find the best function for any job, just open the pointer and you're done.


Take another scenario. Here you have a real big code, such as a mobile phone OS. (not fully open, but compiled). You need to add a bluetooth driver to it for certain equipment.

Now you can add or you can leave the option available in the OS.

You may need to turn on / off Bluetooth from many places.

So, what is the OS, it makes a pointer to a function that includes bluetooth, and uses it wherever it is needed. This code has already been compiled, so you cannot add your code to it. But what can be done, you can write a function and indicate that the pointer points to your function.

This is what I have already seen in Android OS. (not really, but closer)

+6
source

In my experience, function pointers are mainly used to pass a function as a parameter to another function.

Looking at your code, they can also be used as with arrays, so you can just skip the entire array (which may consist of hundreds of function pointers), and it will just execute them all.

+3
source

What is the difference between Function Pointer and Function Call?

This is similar to the difference between the compiler’s request to “tell me the address of the National Gallery (I might want to go there later, and I want to be ready to do it”), and not “take me to the National Gallery right now (but I will not pay attention on how you deliver me, so don't expect me to find out later). " Actually, if you ask for an address / signpost, you can write it down in some place, for example, "next Sunday half of the big trip" ... you don’t even need to remember that this is the National Gallery that you will be going to - it’s It may be a pleasant surprise when you get there, but you will know right away that your Sunday activities are sorted.

What benefits will I get with a function pointer?

Well, as mentioned above, while you are setting the function pointer, you need to make a decision about where you will call later, but then you can forget about all the reasons for making this decision and just know that later all the recipients are ready for use. At that time when you are actually doing something ... "my Sunday routine: sleep until 10, have a big breakfast, come back to sleep, have a shower, if I have a lot of money, then go on my Sunday afternoon big trip , meet friends for dinner "... an earlier solution is just for you to get into the gallery. Most importantly: you can continue to use your familiar Sunday schedule and start to “indicate” the address / “next Sunday day of the big trip” address / sign in new places, as they catch your eye even if they did not exist when your general schedule was created .

You see this post-facto flexibility to change the destination sent one step in the old procedure, well illustrated by the AusCBloke link to bsearch . qsort is another classic example - it knows the general logic of efficiently sorting arrays of arbitrary things, but you need to tell where to go to compare the two things you actually use for it.

+3
source

Function pointers are pointers (like variable pointers) that point to the address of a function. They actually call the base function when you cast them, like a function call.

The main advantage of a function pointer is that you can pass it to another function as a parameter , for example ...

+2
source

These are good examples, especially the first ones in Urvish above. I wonder the same thing, and I think the answer is purely design. In my opinion, they have the same result, since you can point to a function and get + b, or you can just call the function regularly and get + b, and with examples in SO, they are usually small and trivial to illustrate. But , if you had a 10-line C-line program, and you had to change something fifty times because you made changes, you will probably quickly understand why you want to use function pointers.

It also makes you appreciate the design of OOP languages ​​and the philosophy of OOD.

+1
source

Source: https://habr.com/ru/post/917344/


All Articles