Pointers in C Programming | Call by Value Call by Reference | Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is Renoir's so in the last session so we discussed what is the size of pointer in this session we'll see one more important topic in Oh pointers concept so that is what is called by value and what is called by reference call-by-value and call-by-name I will explain with the help of example then you will get more clarity so to execute means water to explain this call-by-value and call-by-name the swapping of two numbers swapping of two numbers we know already how to swap two numbers see once again here it is the concept is called by value here so I am taking the main method void main method here I am declaring two variables a value 10 B value 20 so a and B gets memory allocation inside the main method so here it is a frames also we are writing main frame this is main frame inside that J gets memory allocation at some location and B gets memory allocation in another location and the values 10 and 20 10 and 20 here we are printing printf before swap so before swapping I am just printing a value and B value tab space it will give a value we are printing tab space B value we are printing a comma B 10 20 it will print before swap only just to understand we are printing we are displaying on the console next we are calling a swap method or sphere function we are passing a comma B a and B nothing but values a and B are values call-by-value means calling a function by passing value as a parameter here we are writing swapping logic swap and here it is we are passing two integers a and B are integers so to collect that two arguments we need to declare int X and int Y so can I take a same variables a and B no problem you can take a and B also okay so whenever we call the swap function write swap function gets memory allocation inside that arguments are local variables x and y gets memory allocation here swap function gets memory allocation x and y at some location 7 double 0 2 9 0 5 2 and here it is whenever we are passing a and B a value will be copied into X B value will be copied into y 10 will go and store into X 20 will store into y and next to perform swapping operation anyway we take the help of a temporary variable so here it is we are declaring a temp variable temp x value we are storing x value temp gets memory allocation we are storing X value what is the x value is a 10 10 will go on store here and next into X we are storing Y value what is the Y value 20 so we will go on store into X 20 here and next into why we are storing the value temper we are storing the value M temp value 10 will be stored into y so swapping completed already we discuss this logic this is once again once the swapping has been completed if you want to print print F after swap after swap slash T percentage D slash T percent is d X and why we are printing what is the x value is a 20 what is the Y value is a 10 so after swap it will print the values 20 and 10 yes swapping completed but here it is 1 swapping has been completed from here the control come to here once execution is over the control back to main after execution of every function so control definitely back to main function control back to main here after swapping function calling here I want to print here I want to print after swap message after swap slash T percentage D slash T what we have to print a B or X Y a B R X Y this is the problem and this is the question here suppose if you print a and B a and B values still 10 and 20 only that is the problem next one sir suppose if you are printing x and y x and y are the local variables of a swap function local variables of one function we cannot access from another function so this is the problem in a call-by-value here according to terminology main function is called a calling function swap function is called called function called in a call-by-value in the called function if you process the data that will affect only local variables of that called function only it will not affect calling function variables so once you process the data in the called function once you process the data in the called function that you can access only within the called function here only you can access x and y you can print here but whenever we are trying to access from the column function it is not allowed this is the main problem in a call-by-value concept to avoid this problem what is my requirements are after processing anywhere in the application in any function I want to access from calling function as well as from the called function to avoid this problem to rectify this problem they introduced the concept of a call by reference concept they introduced call by reference right what is that call by reference C so what is the advantage of a call by reference right so over call by value here same swapping program I am using but pointers concept I am using in this first so as usual we are writing main and here it is in the main function I am declaring as usual two variables with a value 10 and B value 20 a and B gets memory allocation here it is in the main function a gets memory allocation and B gets memory allocation with the values 10 and 20 and same story we are printing before swapper before swap I want to print a and B values by giving tab space between a and B we are printing next now I am calling a swap function it is not a call by value it is called by reference program reference means address so we need to pass address of each variable a address B address here address of a address of B this is called by reference call by address here swap function logic we are writing sir what type of variables holding address pointers because here we are passing addresses address of a address of B sir a is of type worth integer so typed pointers only we have to declare integer data is pointing by integer pointer only integer pointer X integer pointer why sir prototype observe the prototype wide function name is a swap it is taking two arguments int easy pointer type arguments are integer pointer type while writing the prototype no need to use the variable names only what type of data it is expecting enough so here swap function is taking two arguments of type int is a pointer same story address of a will be copied into X address of B will be pop it into y so here a swap function gets memory allocation swap function x and y gets memory allocation and along with that of course a temporary variable also to perform swap operation and this is main is called a calling function and the swap is called a called function called function now the 10 will not go on store what value will go on store 2 0 4 6 2 0 4 6 will go on store into X so that it start pointing to this one using point next Y value will go on store into this one I mean B value address of B value 4 0 5 8 then it stop start pointing to this one using point instead of copying data in call by value we are copying the data 20 and 10 but now we are copying address reference we are copying to 0 4 6 & 4 0 5 8 we are coping to 0 4 6 & 4 0 5 8 we copied observe here with the help of that temporary variable we are declaring but into temporary variable in the previous program I am storing X X values are 2 0 4 6 I do not want to store address because it is not a pointer variable I want to store 10 the value which is inside the address who will give the value which is inside the address pointer pointer X X value is a 2 0 4 6 pointer to X value is a 10 10 will go and store into 10 next pointer to Y value will be stored into pointer to X Y value 4 0 5 8 pointer to Y value is this location 20 that we are collecting and storing into pointer to X X value to 0 4 6 pointer to X means what the 20 will go and store into this location 20 next temp value we are storing into pointer to Y what is the temporal you is 10 pointer to Y means what it is pointing to this location so here it will affect observe very clearly where we are processing the data means in the called function where the data changes in a calling function here you are processing but here it is changing the data that is called by reference because there is a connection between a calling function and called function just because of call-by-reference concept because of call-by-reference concept here after swapping I want to print here after swap percentage D percentage D how to print them it is not x and y because x value is 2 0 4 6 y values of 4 0 5 it i want to printer swap at values 20 and 10 pointer to X pointer to Y this is printing in the called function the same value I just want to print in the calling function also same story right after execution of a called function definitely controlled back to main control back to main so in the main function now I want to print the after swap after swap what we have to print a B you can print a B we can print reason here it is a value a lot already has changed 20 and B value is also 10 right so advantages of a call by reference means we are passing address to another person so in the called function whenever we process the information that will affect in the calling function only so that after processing the information if you want to access the processed information in the called function you can access here here you can access and once the control back to the calling function here if you want to access yes here also you can access anywhere you can access this is the advantage of a call by reference over call by value hope you people enjoy this session right in the next session we will see some more examples on pointers for more videos please subscribe to nourish 80 channel thank you
Info
Channel: Naresh i Technologies
Views: 486,274
Rating: 4.9142199 out of 5
Keywords: C Language, Pointers in C, Srinivas, C Language Tutorial Videos, Call by Value Call by Reference, call by value call by reference in c, 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
Id: sj0g6rn-RSk
Channel Id: undefined
Length: 16min 58sec (1018 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.