 Hello and welcome back to this lecture on the use of pointers in C++ programs. Here is a quick recap of some of the relevant topic that we have already studied. We have looked at basic programming constructs in C++. We have looked at the pointer data type in C++ and we have also looked at the address of operator which is the unary ampersand operator and the content of operator which is the unary star operator and we have also seen some of the things that we need to be careful about when using these operators. In this lecture we are going to try to understand a little bit more the usage of pointers in C++ programs and we will also try to understand how the contents of different memory locations change when a program with pointers is executed. So here is a simple C++ program with pointers. We have two integer variables named m and n. The variable pointer int is a pointer to an integer. The variable pointer pointer int is a pointer to a pointer to an integer. Now given this program one might ask that if we give the input values of 5 and 6 note that here I am asking for the values of m and n and I am reading in m and n. So I might ask that if I give the input values of 5 and 6 for m and n then what does the program output when I execute this C out statements. Well, let us see how the memory layout for this program would be. Here I have two integer variables and we know each integer requires 32 bits for its storage. So we need to have 4 bytes or 4 locations in the main memory for storing in each integer. So here is our stack segment where all the local variables of all functions are going to be stored and in the stack segment these 4 locations are reserved or allocated for the variable m which is of type integer. This is the range of addresses for these 4 consecutive locations in the stack segment allocated for m. Only for n which is also an integer we have allocated 4 locations and these could be the 4 consecutive addresses for them. Note that I am writing all the addresses in hexadecimal. Finally I have pointer int which is going to store an address of an integer. So if I assume that all addresses in our computers memory can be represented in 32 bits or 4 bytes then pointer int itself needs 4 bytes and these could be the 4 bytes and these could be their addresses. And pointer pointer int is also a pointer to something therefore it is also going to store an address and once again assuming addresses at 32 bits long we need 4 bytes for pointer pointer int and these could be the 4 bytes in the stack segment allocated for pointer pointer int and this could be the range of addresses for those 4 locations. Now when I execute this statement pointer pointer int is assigned ampersand pointer int what happens I have to find the address of the memory locations assigned to pointer int. These are the memory locations assigned to pointer int I have to find their address. Which address should I pick? There are 4 addresses over here we have studied earlier that we will pick up the address of the first byte in this set of 4 consecutive bytes. So in this case we are going to pick up hex 840 and we are going to store that in the memory locations allocated for pointer pointer int which means in these 4 bytes allocated for pointer pointer int. After that we ask for the values of m and n and read in m and n recall that we were trying to answer the question what happens when m is 5 and n is 6. So let us read in 5 and 6 for the values of m and n note that in the memory the values 5 and 6 which would be input by the user from the keyboard have been stored in the 4 bytes allocated for m and in the 4 bytes allocated for n respectively. Now I execute this statement pointer int is assigned ampersand n what happens I have to first find out the address of n which means the address of the first byte of the 4 bytes allocated for the integer variable n. These are the 4 bytes allocated for the integer variable n the address of the first byte here is hex 780. So hex 780 should get stored in the memory locations reserved for pointer int that is what happens over here and now I say print out star of star of pointer pointer int. So what am I asking the computer to print out I am basically asking the computer to print out the content of the content of pointer pointer int remember the star is the content of operator. So I am saying please print the content of corresponding to this outer star and content of what inside the parenthesis I have another star operator. So it is the content of content of pointer pointer int well what is the value of pointer pointer int that is hex 840. So I have to I have to find the content of the content of hex 840 what is the content of hex 840 that is hex 780. So now I must find out the content of hex 780 what is the content of hex 780 I must go to the address hex 780 and find out what is stored there that is hex 6. So this statement will print 6 followed by an end of line. So this statement is executed what happens I have to find the address of the memory locations allocated for m and in fact I have to find the address of the first byte allocated for m these are the bytes allocated for m the address of the first byte is hex 740. So I have to pick up hex 740 and store it in the memory locations allocated for pointer int. So that is what happens in the memory locations allocated for pointer int I have stored hex 740. Now I am asking the computer to print out once again star of star of pointer pointer int. Note that while this statement asking the computer to print out star of star of pointer pointer int is the same as the statement that we had executed earlier. However the effect of executing the statement now may be very different from the effect of executing the statement earlier because of this assignment that we have made. And in fact we will just see this right now. So what are we asking the computer to do? We are asking the computer to find the content of the content of pointer pointer int and print it out. Well what is the content of pointer pointer int? Pointer pointer int has the value x840. So the content of pointer pointer int is the content of x840. At x840 I have the value x740. So basically I am asking the content of x740 which means I have to go to the memory location with address 740, x740 and I have to find out what is the content over there. And the content over there is x005. I am going to print 5 over here. So you see that earlier when we executed the see out statement we had printed 6. And why was that? Because pointer int at that point was holding the address of the variable n which had the value 6. But now after we have executed the statement pointer int holds the value of the variable m. So when I execute the same statement as I had done earlier I am now going to produce a different output. Because star pointer pointer int just tells me pick up the value of pointer pointer int go to that memory location and find out what the contents there are. And when I read those contents I now get the address of m instead of the address of n which I had obtained earlier. And therefore when I say star of star pointer pointer int I will go to the memory locations allocated for m and I will read out the value there and thereby I will print 5. Now so far we have used expressions like star pointer a to read the contents of memory at the address given by pointer a. It turns out that c++ also allows us to use star pointer a to write the contents of memory at the address given by a. So if I have a c++ assignment statement like this star pointer a is assigned b plus c what this is going to do is it is going to calculate the value of the expression b plus c and it is going to store that value as the new content of the memory at the address given by pointer a. So this is basically saying update the contents of the memory at the address given by pointer a to the value of b plus c. Let us see another c++ program where we use this facility to use pointers to update the value of a memory location. So here is a c++ program which has several integer variables and several integer pointer variables and we read in the value of m and n like we did earlier and then we print out something called sum over here and we might ask once again that if we give values of 5 and 6 for m and n what does the program output? Once again let us go and see how things change in memory as this program executes. So in this case I have mn sum 3 integer variables. So I need to store 3 chunks of 4 bytes in the memory each chunk has 4 contiguous bytes with successive addresses. So these might be those 3 chunks 1 for m, 1 for n, 1 for sum. Pointer and pointer sum are both pointers to integers. They are going to store addresses. Once again we are going to assume that all addresses can be stored using 32 bits. So to store an address to store the value of a pointer variable we need 4 bytes and so we are going to allocate 4 bytes of 4 locations for pointer int as well as for pointer sum and these are the corresponding addresses. And after that we also note that when we declared this variable sum we initialized it to 0. So of course in the location reserved for sum the value 0 will be copied and then after that when we execute this statement pointer sum is assigned the address of sum. So I am going to find the address of the first byte allocated for sum that as you can see from here is hex 7a0 and I am going to store that in the storage allocated for pointer sum. So pointer sum will have the value hex 7a0. Then I am going to ask for the values of mnn and read in mnn. So as we have seen earlier we want to know what this program will output when m is 5 and n is 6. So we read in 5 and 6 for the values of mnn. Note that the values of mnn are stored in the bytes specifically allocated for the variables mnn. Now we execute this statement pointer int is assigned the address of n. So well we have to find the address of n in fact that is of the first byte allocated for n that is hex 7a0 and that must be assigned to pointer int. So pointer int gets the value hex 7a0 and now we are executing this statement which is saying star pointer sum plus equals star pointer int. If you remember this is a compound assignment statement it basically expands to star pointer sum is assigned star pointer sum plus star pointer int. Well what does this mean? I have to find what is the content of the memory location whose address is given by the value of pointer sum. What is the content of the memory location whose address is given by pointer int and I have to add them up. So well what is the content of the memory location whose value is given by whose address is given by pointer sum. Pointer sum is hex 7a0 if I go to this address and find the value there that is hex 000. What is the value at the memory location whose address is given by the value of pointer int? Pointer int has the value hex 7a0. So let us go to that address and let us read off the value from there that is hex 006. So hex 006 plus hex 006 is hex 006. So basically this statement now amounts to saying star pointer sum is hex 006. So we have to update the contents of the memory location whose address is given by the value of pointer sum which is hex 7a0. So we have to update the contents at address hex 7a0 to hex 006 and that is what we have done over here. Similarly for the next statement when we execute it we get the address of m in pointer int address of m is hex 740 and once again when we execute the statement we find the value of pointer sum which is hex 7a0 go to that memory location and read the value from there that is star pointer sum that is hex 6. We find the value of pointer int which is hex 740 go to that memory location read the value from there that is star pointer int that is hex 005 and when we add these two numbers up we get 11 which is hex 00b and so now we have to update the contents of the memory location whose address is given by the value of pointer sum that is hex 7a0 to hex 00b. So we go to the location hex 7a0 and update the value to hex 00b and finally when I am required to print the value of sum I go to the memory locations allocated for sum read off the value there which is 11 and print 11 in decimal. So in summary in this lecture we have looked at the use of the address of and content of operators in C++ programs we understood how the contents of memory locations change when executing programs with pointers and we also saw how the content of operator can be used to update memory locations. Thank you.