Function Pointers In C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys they are watching TV peanuts video on function one we will try to answer these questions here like what is function pointer and how to create them and calling a function pointer using a function 500 self and last what is the actual use of function pointer where we should use function pointer not anything else okay so stay with me and let's start it so first of all what is function pointer so I have these questions and answer here so as we all know that no other pointers variables store the address of another VD okay so if you are writing something like this teacher I tend in theta point of P so this point we will hold the address of Pi okay so we all know that very well okay now what is function pointer if it is function pointer it will be storing the address of another function okay so first do you remember that now the question is how to create them so we'll take the example of this normal pointer itself so let's suppose you have some function which will add two numbers and return the addition now you're going to create a function pointer which will store the address of this pad okay so how do you do that so let's take an example of this one here you wanted to store the address of I so you created in PHP Point C you didn't create it a character pointer and store the address of I know you did this okay you created integer pointer and store the integer data types address inside that okay so if you want to store this type of address then you have to create this type of pointer to hold this address and what do I mean by that is the function pointer what you are going to create so the return in teacher and should take two integers like this as a parameter and it is really very simple to create function pointers why because what you did in case of normal pointer here you just created a variable and at front of the name you just put this star mark okay so we'll do the same with function suppose you have this function name and it is returning to integers and you will write this pointer at front of this function name but hold on guys this is not done if you will see something like this what will you think there is a function that function is taking two arguments as integer and returning in p/j pointer it is looking like you're returning into your finder this is not looking like a function pointer so this star or a string should be with this name so how to associate this this is the way and you are done now it is function pointer and you can happily add I mean assign this add here okay so let's see whether this code is really a working code so this is your program this is the add function and we are creating a function pointer here so let's compile this so this is GCC programming is then we compile it there is no issue in this program okay so what we have learned is how to create a function pointer here okay so what is doing actually it is holding the address of this add function and there are two ways to do this I mean I just literally forgot to mention that but I should mention that as well there is this way of assigning the address of a and this is another way okay so this is most perfect way because this will work even with the old compilers this is not going to work with holes or buyers okay so better you should always use this one if you want to migrate from new compiler to old combined now as this ad is here now you can use this function to call this function that's the only thing what this function pointer is meant for okay you will be calling some function with some another me okay so you are calling this add function with this function and let's see how so if you want to call some function here you simply pass these two values you will get some value place here and we can just simply go ahead and bring that so let's compile this code there's no problem if you run this the answer is 3 so 1 2 & 3 so you notice that your function pointer thing is working but wait a second this is a new way to call function and this is the traditional way okay so I told you how to assign that by using address operator and how to call function there was this that simple way and this is the traditional so let's check this out that this traditional is working and yeah see it is working okay now we know how to create function pointer and we know how to call them okay and as I told you that your function pointer should be of type exactly what you are going to assign in that okay so this add is of type returning integer and taking two parameters that's why your function pointer is also returning integer and Ageing to integer parameters okay so if you will make some change here let's suppose you are adding one extra integer in that case if we compile there will be a too few arguments to function fun okay and if you will add another argument like one two three and you cook and compile so here you got the warning because wait a second and the result is three so this is what I wanted to show you that if your pointer is of not the same type what you're assigning into that is a problem here okay see the warning here so you should avoid using this because see this third value is avoided here okay only one and two are passed and you're returning is the addition of one and two here okay so far we have learned about function pointers on how to create okay now there is an another question and that question is this calling a function using a function pointer we already saw this so we will skip this question now there is this another question how to pass a function pointer as an argument to another function so let's create another function here so let turn understand what is happening yet you have one function function one and you're passing this ad which means you are passing the address of this ad function to this function and you are catching that address like this okay so this is how you write in function parameter when you are getting some functions address okay so let's run this if you will compile this okay I'll forward to have this cesium okay so let's compile game there is no issue if you run this as it is still three okay so this function we are calling add function here with some function name and that some function is getting created like this now we will see another question here and that question is how to return a function point and this is really very interesting question you it was so easy right what function type you want to return these both functions prototype is same I mean both are taking two integers and returning one integer so you created a type of the same prototype here now let's look at the another one that another one is little tricky let's suppose you don't want to use this type tap and all then how you will deal with this so this is your function which will return a function pointer of this type okay so let's do that then this type is a returning integer and taking two integers so first of all you will enclose this function with one round bracket here like this so this has become the name parameter integer an integer and the return type was indeed and one more thing this point in your set now there is this function at U n which returns a function pointer of time taking two integer as a parameter and returning one integer okay so this is how this whole thing will work so let's compile whether it is working or not say it is working and the answer is still three I know this is a little tricky and hard as compared to this type def thing and that type that was more readable than this okay so if I remove all that see this function is a function which will return this math function type okay this is a type not a function okay don't get confused you always write type here like India and character okay so if I am writing this math function that means it is a type and we are defining a type like this using type there and the syntax to define a function pointer as a type is this okay I know this is a little bit hard to understand but once you will practice two three times like pass some function and return some function and try to do all those things what I taught you here it will be easy for you to remember all these things okay so we have seen how to return a function pointer now there is a question it says how to use arrays of function pointers so now I will create areas of functions you so see this is really really easy right that you are creating an array of two sides and the type of the arrays mat function which is nothing but a function pointer return integer and take two integer as a parameter okay that's why we are assigning these two functions of this type into the air okay and this is the syntax of calling that and yeah this is new syntax actually you should call it like this and this pointer and this punch okay so let's compile this as well there's no problem and the same answer okay now this was a very neat and clean way to create an array of function pointers because you have this type created already using type def what if you are not using this type def and you want to create an array which is stores these two functions of type this in that case you will again do the same thing you will wrap this put this pointer here now you do integer integer Commendation what we did before I told you to wrap whatever that function was when we were learning returning a function from another function I mean they turning a function pointer from some another function that time I told if you are not using that type def then you will be wrapping up your function and write the type like this so here you wrapped up your array with this pointer sign and return type like this okay and now we are good to go let's compile this this is compiled and running it is working okay so now you are not using this type depth thing and all that but I would recommend using that because that gives a cool readability this is so much confusing right so we have seen how to create an array of function pointers okay like this let's look at the next question here and that question is where do you use function pointers this is really very important question so I have a program for that and the best use I can say is you will pass your functions address okay to some another function and there you will use this function okay or any function you want to use so the best uses in C we know there is something called Q sort which is quick shot this is inbuilt function which will sort your array so let's suppose you are having some array you will pass that array inside this Q sort and this Q sort will sort that adding and there are a few parameters you need to give and lastly you have to give the address of your function which will compare the elements okay so Q sort need a function to compare two elements okay so let's run this and find whether it is really working or not so this is this is a demo and now if you will run this C one two three four five so you give this array in quick shot quicksort sorted out the elements inside that with the help of this compare function and after that you were just simply printing the whole array okay and as I told this is making the ascending order if you will make like this like left hand side is less than right hand side and then if you will return true then this will become descending alright let's compile this and check this again see first it is printing five four three two one before it o is one two three four five okay so we can clearly see that this is playing a major role here but that is not the point the point is Q sort need one function as a parameter okay this function is a user-defined function this compare function because sometimes what happens you are sorting some structure this is integer value so it's okay but they suppose your sorting some structure or something that in that case you will be passing to structure pointers you will typecast that into the structure and inside that structure there would be some filled on that basis you will be sorting that whole structure okay I mean area of structures so in that case this compare function becomes really handy so that's the only reason this compare function is given to us I mean we're supposed to implement this function the rest of the things are taken care by this Q sort but the compare thing is given to us okay because our data could be anything it could be class or anything okay so to recap that the best use is you will be passing your function according to your need this is really very good example to understand this okay so I think you got the video if you liked the video don't forget to hit the like and subscribe button if you haven't subscribed yet I'll see in the next video
Info
Channel: CppNuts
Views: 27,448
Rating: 4.8396435 out of 5
Keywords: Function Pointer In C, function pointer in c and c++, function pointer, how to use function pointer, what is the use of function pointer, how to implement function pointer, c programming, c tutorial, function pointers with example in c, programming tutorial, c++ lessons, how to code in c++, c programming language course, pointer, code, coding, programming
Id: je7aPo0JScU
Channel Id: undefined
Length: 16min 3sec (963 seconds)
Published: Sun Dec 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.