Pointers in C Programming | Pointer to Function | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to nourish technologies in this session we will see a pointer to function how to create pointers to functions generally we know that how to create pointers to primitive data types such as character integer float double so now it is possible to create pointers to functions also but one thing if you want to create a pointer to function the declaration is completely depends on the function prototype the declaration of pointer completely depends on the prototype of the function observe here suppose I am declaring the syntax Palelei we will see that example also example identity is nothing but pointer name that identity you have to place inside the parenthesis and next return type return type and next one arguments lists how many arguments it is taking this is declaration the syntax of pointer to function is here it is first identity identity you must place inside the parentheses along with a pointer and the return type and next arguments list so see one example so this is functional pointer so now I am writing function function pointer so first function is important take any function suppose function is add it is taking two arguments integer or Demon's int X and int Y it is just performing addition operation and it is returning the value Z Z is of type word integer type function is ready function is ready here to create a pointer to this function right variable how to write identity any name you can give I am simply giving PTR and here it is a pointer pointer and next here we need to place inside the paranthesis arguments depends on prototype of the function how many arguments this function taking two integer arguments so two integers to integers and what is the return type of the function integer this is return type pointer pointer variable is ready function pointer arguments list this pointer can points to any function not only this function that is just example pointer to any function which is taking two arguments which is taking two integer arguments and returns integer data taking two integer arguments returns integer data such type of functions any function this pointer can points to this is pointer to function declaration this is pointer to function declaration so based on this so I am writing a simple how to write observe so first we are writing to functions in the program one is a integer add function same function we are using int X and int Y into y and here it is Z equals to X plus y and it is returning that Z but next here it is we are taking one multiply it is taking three integer arguments three integer arguments in W equals to X is multiplied with Y is multiplied with Z and this is returning W value observe two functions we are taking add function and multiplication function add function is taking two integers return type is integer but multiplication is taking three integers and return type is an integer now based on this we are writing program observe main function we are writing main function main now we are declaring one variable suppose our one to store the result our one we are calling add function direct calling we are passing values 10 and 20 that we are collecting into our one next we are calling multiply function we are passing 3 integers 2 comma 3 comma 4 the result we are collecting into are 2 so our two variable also we are declaring and here we are printing our 1 value our 2 value r1 comma or 2 so what are the values it will print our one value is a 10 or 2 value mean 10 into 10 plus 20 sorry 10 plus 20 30 it will print 30 and next what is the r2 value 2 into 3 into 4 3 into 4 is a 12 into 2 24 it will print is okay direct calling but now the concept is a creating pointer to these functions and accessing I am declaring only one point one functional pointer I am declaring observe here I am declaring pointer variable variable pointer variable it is a local variable so you should declare at the top local variables rules here PTR PTR and here it is arguments we are taking only two integer arguments and the return-type is a integer sir this pointer can points to only add function yes it can points to only add function it cannot points to multiplication function reason the prototype is different that is my intention that I want to explain so here into this PTR first we are storing address of add because PTR is a pointer variable it holds address it holds what address whenever a frame will be created to a function whenever method space will be created to a function the frame will be created at a particular location this is called entry point of the function execution using PTR we are storing that entry point to point into this one store to access that function so whenever add function address we are assigning add function address means the framer dress the memory will be allocated inside the RAM to execute that function of course inside the stack memory that address we are collecting into pointer that we are assigning to PTR and if you want to call instead of using address we are using PTR PTR we can pass 30 and 50 it is pointing to this frame it is executing this frame it will return that at value that we are collecting into r1 once again because working with or one is over if you are confusing just declare one more variable are three and we are printing printf percentage dr3 value what is the Tatra value 30 plus 50 80 it will print output is a 80 it will print okay up to here it is happy direct calling here is allowed next with the help of pointer we are calling it is also allowed but now the concept is into PTR variable whenever we are trying to store the address of multiply of multiply you will get error message right here it is giving that error message error message incompatible pointer assignment incompatible pointer assignment we are trying to assigning the address of multiply to PTR but the problem is PTR can points to a function which is taking two integer arguments and it is returning in teaser but here it is it is taking three integer arguments and it is returning integer this is not compatible these are not matching for in turn and multiply function prototype are not matching so this is not possible you can create pointer to function but only one rule you have to follow that the prototype of the function and the Declaration of a pointer variable should be same exactly equal how many arguments you are passing signature and what is the return type both should be match if it is not right so definitely you will get error message simply incompatible pointer assignment here it is the simple question will rise the simple question so what is the simple question means sir why a pointer variable identity we are placing inside the parentheses in the previous cases whenever we discussed all the concept examples nowhere we are placing the pointer identity inside the parentheses then why you are placing C whenever so we are writing actually this is declaration exactly declaration will be integer PTR taking two integer arguments so rewriting as rewriting as in teaser I am just removing the parentheses the question is sir if you remove these parentheses what will happen nothing but why you have to place the identity inside the parentheses whenever you rewrite this declaration like this then system considers or a compiler or executor considers as follows in teaser it will write here integer pointer come to here PTR integer comma in teaser means the complete declaration has changed Hauser now PTR is not the point here PTR is the pointer is pointing to the function which is taking two integer arguments and it is returning integer that just now we have discussed but here PTR is a function PTR is a function which is taking two integers which is taking two integers and what it is returning returns address of integer variable address of integer variable so that is why integer pointer is a return type the complete meaning has changed the complete meaning has changed PTR is a function it is not a pointer the function is taking two integers and it is returning integer pointer so what is the situation where we use such type of examples that is a dangling pointer concept so what is a dangling pointer concept this is also very important concept that we will see in the next session okay so this is clearly about a pointer to function concept and if you change if you do not use the parenthesis in the Declaration of a pointer to function right so what it will become how the system treats right this example we will see in a dangling pointer concept that is in our next session hope you enjoy this video right for more videos please subscribe to nourish id channel thank you thank you [Music]
Info
Channel: Naresh i Technologies
Views: 337,805
Rating: 4.8865447 out of 5
Keywords: C Language, Pointers in C, Srinivas, C Language Tutorial Videos, pointers in c language, c pointers, c pointers programs, C Srinivas, learn c language, c tutorial, c programming, pointers in c programming, pointers in c programming tutorial, Pointer to Function, pointer to function in c, pointer to function program, simple program for pointer to function in c, typedef function pointer in c, use of function pointer
Id: 82NrXp8RR0k
Channel Id: undefined
Length: 15min 39sec (939 seconds)
Published: Mon Sep 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.