C_101 Function Pointers in C | Pointer to Function | C Programming Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning c programming we are discussing functions in c in this video we'll discuss what is a function pointer see we have other pointer like integer pointer float pointer character pointer same we have function pointer and what integer pointer will contain address of a variable whose data type is integer right float pointer contains you can say a float pointer or a pointer to float right same we have function pointer function pointer is also it's a variable which contains address of function now how it is going to contain address of function and and you can dereference it also as we can differentiate reference other pointer integer float or character and we can get value you can dereference this function pointer also now how you can use this function pointer how you will dereference it we will discuss that thing with complete program here with proper example and we'll show you i'll show you practical also we'll run that that code also right and the function pointers are very important you know they are having some great applications that also discuss the use of function pointer right in detail but before they just want to tell you one thing if you are looking for a great career in software development then you must think about the aspects like what's trending in the industry and the kind of questions being asked in the interviews how to improve your course your ranking what is the thought process behind the great application like amazon flipkart zomeito ola so here an academy brings you a platform where you can get access to weekly shows which you can watch live and the host of these shows are working in some of the top companies like linkedin amazon google and our seven star coders on code chef and industry experts with years of experience so they'll be covering the content which would be really helpful for your career like in the live episodes you get an opportunity to ask take a chance you know about the top 20 questions being asked and the industry leaders directly about the recruitment process in the top startups and mncs what is the eligibility criteria and how to apply for them even you can get your resume reviewed by the exports stes not only this you can also participate into mock interviews and you can learn courses on programming languages cloud computing blockchain and crypto tech aspects of digital marketing and data analytics and if you want to know how to apply for the jobs in service based companies what is the entire hiring process where to apply how to apply how to study for the test and what to prepare for the interviews then there is a show for you in which you will get in-depth information about the hiring process what type of questions are generally asked and how to make sure your resume stands out from the crowd so the detail of the show and the batch you will find in the link that i put in the description box of this video and don't forget to use my code jkl10 to get 10 extra discount on your subscription so now let's see what is a function pointer i hope you know what is an integer pointer how to declare that pointer int asterisk and pointer name ptr that's it it's an integer pointer you can say pointer which is pointing to in or a float pointer means just write down the data type float strict the sign and the pointer name that's it now how to declare a function pointer same you have to write the return type first then we will write s trick then we will write the pointer name here you will write pointer name right and as it is a no it's a function pointer so function means obviously we have some maybe we have some argument or maybe it's blank right so here also we have to write something and you have to put this thing also into bracket i'll describe this thing one by one clearly right so what you will write here here we will write what return type of function whatever the return type of function exactly that type you will write here when you declare a pointer then in brackets you will write a string pointer name and here what you will write you know data type of the arguments how many arguments you want to pass in that function the data type of those arguments or if you don't don't want to pass any argument just leave it blank now let's take a simple example how to declare see i'm taking a function i'm just taking a function in and sum i want to do sum of two numbers i want to pass two numbers as an argument so in a in b and here simply i want to do return a plus b that's it this is what you can say definition of this function so how to declare this function prototype would be what int name sum and here ain't comma into this is what declaration of this function i hope you are getting these you have discussed this thing many times this is definition this is declaration now i want a pointer which points to this function now how to declare that pointer see that is very important it depends on the prototype of the function so what is the return type of this function in so first you will write what hint fine now asterisk pointer name i am taking ptr only and you will put this into bracket and here what you will write data type of the arguments how many arguments i want to pass two arguments or here you can see and data type of both arguments are in so here you will simply write in comma hint this is what declaration of a pointer which is pointing to this function right if you write here void or rather than word i am just first of all we will initialize this pointer let us initialize this pointer it will contain pointer is what a special variable which contain address of other thing so this is function pointer so it will contain address of this function so how you will initialize it equal to address of name of the function name of the function is sum so simply you can write sum this is what initialization of this function pointer that's it and you can call this function now using this pointer you can dereference it right now see why i'm putting this in bracket if you don't put this in bracket i am writing into strict ptr in comma i am writing simply this thing so now here see we have a streak is also an operator and this is also an operator these brackets and which is having higher priority these brackets are having higher priority than this asterisk it means this pointer would first of all belong to this or you can say this operator and this operator both are fighting for this ptr this operator is saying this ptr belongs to me and this operator is saying this ptr belongs to me so to decide check out the precedence precedence of this bracket is higher it means this bracket will win and this ptr would belong to this bracket so this statement means this pointer is a function because it belongs to here this this one first note this so pointer is a function which takes two argument integer argument and return and this function return a pointer to integer this line means this thing right but if you put this into bracket now this is also bracket this is also bracket both are having same precedence now check out the associativity associativity of this operator is left to right left to right so when you move from left to right this operator first comes it means now the pointer belongs to this this will execute first so now it means ptr is a pointer see that you know the difference in the explanation ptr is a pointer or you can say ptr is a function pointer which accept two arguments of both integer type and return an integer value return only an integer value i hope you are getting the difference between this and the previous thing because sometimes they ask this type of question also in gate also an interview also they'll write down one line and they'll ask what is the meaning of this line so please check out that thing clearly it's very confusing when it comes to pointer i hope you got the difference between these two lines right so that is why we put this function pointer when you declare function pointer then we put this pointer name into bracket right otherwise it will mean you know a different thing so now let's write down a program in main function first we are declaring it then in main i am writing this main function and obviously will take one you can say variables s in which you are going to store the result because because it is going to return integer value so that when we will call this function that we will store in s so now we will not call this function using the function name we will call it using the pointer name because pointer is containing now the address of this function right so now how you will call this s trick just write down the pointer name in bracket and pass obviously you have to pass to argument so i am passing 2 comma 3 that's it and just printf percentage d and whatever the value in s and after main i am writing this definition here that's it so now how it would be executed see so first of all this obviously this program would be this is source code in high level language you have written so this will be converted into binary code like 0 and 1 all the instructions these are what instructions all the instruction would be by the compiler would be converted into the object code then the executable code and then obviously it is going to run so whenever you run this that executable code that would be you know you know moved into the main memory ram right so here i am writing this definition of the function here now see what will happen obviously some memory would be located to this this would be stored in main memory the executable file when we are going to execute it so all the instructions are having all the instructions are having some memory address obviously these all the instructions are having some memory address right somewhere this function is also having memory address so suppose here this this function is here we are having this function the function the instruction one of this function you can say instruction two instruction three how many instruction they have all the instructions are here sequentially they are going to be placed in the memory and they are going to be executed sequentially by default right so now suppose the starting address of this this function is starting addresses thousand starting address or you can say base address of this function is thousand right obviously pointer will contain the base address only we will deal with base address only right now suppose base address of this function is memory is 1000 so now what we are doing when compiler will go here in the main function some memory would be allocated to this main instead obviously and there we have one variable s that is having 0 and here we are having a pointer also we are having a pointer this is a function pointer which is containing address of sum address of this is what what is the base address thousand so it will contain thousands so now this pointer is pointing to this block so here function pointer contains address of code this is what code and other pointer like we were taking integer pointer that was containing address of a variable or you can say address of the data in that variable suppose we have value 10 so that pointer contain address of that data that value but function pointer contains what address of the code executable code this is the code not address of any data address of the code right so now using this pointer we can access this function we can dereference it so we can call this function also now here what we are doing a strict p first we are going to dereference this means ptr means value at this value and this is what it's not value its proper code so now first of all we are going to dereference this function and then we are going to pass the argument to three so 2 3 would be passed and now in sum in sum sum memory would be for the sum obviously so we have a and we have b so in a we have 2 where we have 3 return a plus b that is 5 it will return five so here it will return five and some we have five so it will print five this is how using pointer you can access ah function right another way is rather than this ampersand is optional you can simply write the name of the function name of the function also you know means the address of the base address of that function so if you will not write this address so that is also fine if you will not write this strict opera operator here that is also fine simply write ptr23 it will also work and it will give you five so this is very popular second one rather than using address of operator and this s2 operator right so i hope you go to what is a function pointer right the basics how to declare it how to initialize it how to use it in detail we'll discuss in the next videos then a use of function pointer why we use function pointer that is very important right and you have to take care of this thing this this declaration of this function pointer basically depends on the prototype of the function return type should be same here the data type of the arguments number of arguments should be same if you write down here one int it will give error because here we are passing two in here you will write one into one float that also will give error this pointer cannot contain address of this this function right because here we have end here we have float but here we have both the end so it will give error right so all the variations i will show you practical we'll run this and we'll see what kind of output you are getting right so now let's create a new file named function pointer and here we are taking one function that is int and name is sum i am just declaring that function here right and in main obviously we will call this function and write something but first we are going to write down the definition of this function so here i am writing what end sum intake comma and b you can take another example also it's not like that you have to take the same example of like sum of two numbers it's simple that is why i am taking this example return a plus b right and in main now what you have to do in s and just to declare a pointer now return type is end here just in the bracket write the pointer name i am taking ptr and in bracket what the data type of the arguments how many arguments two and three type are integer i'm initializing at the same time is equal to address of address of what function name is sum so now just call the function how you will call s trick ptr and just pass the argument suppose i am passing 1 comma 2 and whatever it will return we are going to store that result into a variable named sum sorry s and just print sum equal to percentage d and whatever the value is that we are going to print right i hope it will work and let's run this now it should return 3 i mean it should print 3 right see it is printing sum is equal to 3 so it's working we can dereference we can you know access the function using pointer i am not writing address off and here i am not writing what this s trick lets see now it will work or not let us run this again see it will work fine sum is equal to three so you the same percent and this s trick are you know optional in this case so in the next one we will see some more points about function pointer what you can say what are the applications of function pointers and i'll see in the next video till then bye bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 8,578
Rating: 4.9295774 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, what is function pointer in c, c programming tutorials for beginners, what is pointer to function in c, what is function in c, advantages of functions in c, gate cs lectures, previous year gate question with answer, c jennys lectures
Id: qaszuaFXRTA
Channel Id: undefined
Length: 18min 3sec (1083 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.