Why are function pointers useful?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i want to take a look at why are function pointers actually useful because i take a look at how you can use them how to initialize them how to declare them how to then call them link up top if you want to watch but i didn't show you any sort of practical use case for them and uh i'm going to take a look at how they are actually useful first things first i want to create here a simple example let's say we have uh two functions right we have here an inked sum just a function that does summing between two numbers right simply returns x plus y and i have another one uh that returns the product right so we have here in y and let's define it properly x times y and what i want is just a simple problem takes into a random numbers and does something with them so i'm going to include here time dot h times h and i'm going to initialize here the random generator so s trend of time of no and let's say here in a equals rent percent 100 and let's say equals percent 100 and what i want is a simple function says printf the result of the operation between between percent d and percent d is percent in these guys are going to be the actual numbers and i have a b and actually have an enter here it's going to be a b and uh whether we have some or prod that depends on something so we're going to have here some between a and b okay so that's fine we have uh this program and if i launch this of course of course i'm gonna get two numbers so 59 and 32 and they are going to be sums together so that works properly as expected if we change it to prod that's fine as well right um we're going to get a problem between some pretty big numbers actually so that's kind of difficult to check but let's assume that that's the right result now here comes the kicker what if i have this code i don't want to change it i have it inside a library somewhere outside of this project i want to use it but i want to change something to it i want to change the operation right so i want it to do everything the same but this operation has to change has to be whatever i tell it to be well in that case we can use function pointers first things first let's create a simple function that says should not be changed so this function really we should not change it right now if we don't want to change it then how do we actually change the operation here the answer is actually function pointers we can have here a function pointer inside arguments and have it be a function pointer that returns an integer right let's say like that and let's call it operation and then it's going to take in two integrals so it does return an integral it takes into integrals and if we take a look at the functions up top indeed we take in two integers and we return one of course so that is the function point are defined then we can instead of calling plot here we can call this function pointer with a and b and to call this function that should not be changed what simply we have to do is just pass in a function pointer to either prod or sum right so now if we try to launch this we're going to get the product between the two so is that and if we change this to sum we're going to get the summation between those two numbers so that is how you can use these and this is the real basic idea of every use case out there you have a library out there that needs to do something just a tiny tiny bit different right everything else should be the same just a tiny bit different here the operation and you have to somehow be able to change it and those this is where the function pointers are used now let's take a look at the actual use case so here on my website i have the handling signals lesson and in here if we copy the code and take a look at it in the visual studio code here we can notice that we actually do use function pointers and this sig action here that we have defined to handle our signal was being passed a function pointer if we take a look at it at its signature well it's going to be a bit trickier to find it but i think if we go here we might find it is there we go so sync signal t signal function and there we go this is the actual signature of the function we have to pass in it a function it returns void and takes in an integral similarly for our intro to threads so intro to threads here we had a lesson that also started with function pointers so this code here was actually using function pointers to tell the thread that we started so here we have pthread create creates a thread that executes something and that something is the function pointer that we pass in here of course this should be actually with avoid pointer arcs maybe because inside pitot creates the function pointer actually let's go here the function pointer is defined like so it does return a void pointer and it takes in a void portal right so these are at least two places where function pointers are used and there are many many other examples that you can find out there okay i hope this was useful uh i hope you got an idea of why these are uh these function pointers are kind of interesting and you can do a lot of things with them if you really try it and maybe this actually sparked a bit of interest in you and you could create a program that makes use of function pointers that would be cool to see but otherwise i hope you enjoyed this lesson and if you do have any questions leave in the comments below or on our discord server again the source code can be found on our website as you saw before link in the description below take care bye
Info
Channel: CodeVault
Views: 4,186
Rating: 5 out of 5
Keywords: codevault, c (programming language), function, pointers, use, case, useful, why, what, to do
Id: ewBBRaF0oEA
Channel Id: undefined
Length: 6min 43sec (403 seconds)
Published: Fri Jan 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.