What are function pointers in C?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in today's video I want to talk about function pointers what are them and how you can use them well what function pointers are is basically a way to call anonymous functions that are only known at runtime something like I know passing a function to a function that does something with that function if that sounds confusing let's get straight into the example so first to prepare the ground I'm going to create two functions one that's going to add two numbers and one is going to multiply two numbers right it's going to return a long wrong I'm going to call it add and I'm going to take in two integrals simple enough and same thing with multiplying all right so now well we can simply call these functions if you want I can just simply printf this I'm gonna print lld or % LD since the return type is long long and it's a sign it's assigned long long so that's what that ll stands for and I'm just gonna call one function let's say add at five and seven right simple enough we should get twelve and that's what we get right so far so good now what if we want what if we don't know the function here that were calling what we know that function has two arguments both the both of them are int and the function itself returns a long-long signed integral well you can do that using function pointers and here's how you define a function pointer similar to a pointer but not call it so first is the return type so say long long right both of our functions are gonna return long long so it's a long long and then in inside brackets you start with an asterisk and then you add the identifier let's say for the sake of abstract icing everything I'm just going to say fun from function right and after the closing parenthesis here can add another another pair of parentheses in which you list every single parameter right so basically a copy-paste of this but you can even copy paste this and just remove the names of the variables we don't really need those right so that's how you define a function pointer how do you read that it's fairly simple once you get the gist of it so we can recognize it's a function pointer because we have an identifier that is preceded by an asterisks and inside the parentheses but simple enough what does that function pointer return well it returns a long long right and what does that function pointer taken as input well it takes in two integrals and now we have to assign it a value right right now it's just a function pointer that uninitialized we are not pointing to anything right and that's very very simple because the notation is incredibly similar to when you are trying to assign a pointer a certain value so what we do here is say well the identifier of our function pointer which is fun equals and then we say whichever function we wanted to we want to assign to it right and I'm going to choose here mol right our multiply function and we have to preface this with an ampersand right so what this says is the value of the function pointer is going to be the address of the function multiplied right and now if I try to call this here instead of and I'm going to call it with the fun function pointer and you notice there's no other function called fun in here if I run this we'll get 35y simple because five times seven is actually 35 and we can without changing the actual call of the function we can change whatever is being used here so you can say fun equals the address of ADD and now all of us the result is 12 and you can do this at one time which is absolutely amazing you can simply say okay I'm gonna first printf this one right and then I'm gonna change the value of our function to mul so I can actually print have a different value in this case I'm gonna get both 12 and 35 right so that's how you can use and define function pointers there's a very simple concept and while it might not have that many usages you can actually pass it in as a parameter to a function just like so if I have here a void exact function I simply can copy this part and paste it in as if it's an argument right and then I can call it right in here just like so and to use it we just simply say exact all and we give it the address of a function we can say exec of ad and an exact of Mull with an ampersand at the beginning if we run this you'll notice that we get both results oh yeah so you can see why that can be useful when passing certain event handlers and other callback functions that you might want to be called after something is has finished executing right that might come in useful sometimes in your C framework for example all right so that's about it for today's video thank you guys so much for watching if you have any questions leave them down below and see you guys next time bye
Info
Channel: CodeVault
Views: 4,390
Rating: 4.9838057 out of 5
Keywords: function pointer, function pointers, c programming, what are function pointers, how to use function pointers, function pointers in c, function pointer in c, pointer to function, how to pass functions as arguments, passing functions as parameters, passing functions asarguments, codevault
Id: cwvdT-4HT9o
Channel Id: undefined
Length: 6min 5sec (365 seconds)
Published: Wed Feb 13 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.