 So, what we will do is we will quickly go through the next two lectures in the series. And as usual if you have doubts please raise your hands and ask questions. And then we will have the usual video recap quiz and then practice questions. So, this lecture is going to be about pointers and addresses of memory locations. And the difference from what we have been seeing so far is that we have been accessing memory locations through names. So, accessing memory locations through names is what we have seen so far when we use variables we are basically providing a name of a variable and that refers to a particular memory location. And what we are going to see in this lecture is that how can we access memory locations through their addresses. So, I am sure all of you have gone through this video lecture. So, basically memory can be thought of as a sequence of locations and each location has some contents. So, these are the contents each location stores one byte that is 8 bits and each location also has an address. So, here the addresses are shown in hexadecimal. And when a program is running basically the operating system allocates a certain part of the memory for use by the program and that memory is divided into three parts the code segment, data segment and stack segment. So, this we have stack segment we have already seen this is where the call stack is this is where activation records get pushed when you when a function gets called. But the data segment we have actually not seen it so far we will get to see it in a couple of more lectures use of the data segment. And the code segment is where the executable instructions are stored. So, this is the memory allocated for a process. And here is a simple program and what we are going to try to do is to try to understand you know how do these different variables what do they correspond to in memory and when we store some value in them what exactly happens. So, you know in the Dumbo model of computing these are like the named drawers of Dumbo and because these are local variables of main they will be allocated in the activation record for main which will be in the stack segment in the call stack in the stack segment. So, when we talk about the variable a we are actually referring to a location in the activation record of main in the call stack in the stack segment. So, the space for a is really in the stack segment and since these are memory locations and we just saw that every location memory has an address. So, can we ask what their addresses are and one could sort of visualize it this way that if in the main program I am declaring a variable a which is of type integer how much storage would be needed to store the value of a 32 bits and integer is 32 bits each memory location contains 1 byte that is 8 bits. So, 4 memory locations would be needed and all of these are local variables. So, they should be allocated in the stack segment. So, 4 bytes for a in the stack segment and which basically means that each memory location storing 1 byte. So, we will have 4 locations in the stack segment reserved for a and similarly for b float again requires 32 bits. So, of course, the representation of a number in float is very different from the representation of a number as an integer. So, the same 32 bits might be reserved for both a float and an integer, but the way numbers are represented very different float is the mantissa exponent representation and integer would be 2's complement and similarly for c which is a character we need just 1 byte. So, you see that you know what we had taught several classes back about how many bits are needed for each of these different data types is actually important when you try to understand what is happening in memory as you know as you try to run this program. And then one could think of addresses for these different memory locations. So, this is 1 byte this is 1 byte allocated for the character it could have this memory address the addresses are written in hexadecimal and then there are 4 bytes here for float and the corresponding addresses could be x200 to x203 and finally, for the integer these 4 bytes. So, note that it is not necessary that because a is declared in the program before b before c it is not necessary that I mean we should not make any assumptions that the memory address of a would be less than the memory address for b would be less than the memory address for c this is not at all necessary. So, and when we write some code like this we are basically accessing the memory locations by their names when we say read something from the input and store it in a we are saying access these memory locations by their name the name of that was a. So, now the question is that can we access memory locations by their addresses and the answer to that is a yes and this is how we can try to find out addresses and then access locations to their addresses. So, c plus plus has this unary ampersand operator that is that and sign it is the ampersand symbol and what it does is if a is a program variable then ampersand a will give the address of a in memory. So, as we have seen here that a program variable actually corresponds to program variable corresponds to some locations are reserved for it in memory. So, when I put the ampersand operator I can actually get the address of those locations. So, this is really coming close to what the physical memory is about there are addresses and now you can see the addresses using the ampersand operator and this ampersand operator is a unary operator it takes a single argument and you can write ampersand a or ampersand space a whatever and you know just like when operators are applied on operands you get expressions like a plus b is an expression. So, ampersand a is a c plus plus expression and whenever you are applying operators to operands to form expressions you need to worry about operator precedence I mean for example what would happen if I write ampersand a plus plus is the plus plus to be applied to a first and then ampersand to be taken or is the ampersand to be applied to a first and then plus plus to be taken. So, you need to worry about the precedence and associativity, but we will just simplify our lives and say that we will use parenthesis to make it very clear which one we want to do first ok. So, we just saw that if a is an integer variable ampersand a is an expression now every expression in c plus plus has a type. So, what is the type of ampersand a? So, that type is called pointer to an integer and in c plus plus it is written as int star. Similarly b is a float variable ampersand b is an expression whose type is float star pointed to float. So, if you have a variable of type t, if x is a variable of type t then ampersand x will denote the address of x and it will have type which is a pointer to t or t star. Now, so far what we have also seen is that whenever we have some type like character float integer whatever bold we can also declare variables of those types. So, if int star is going to be a type pointed to an integer is going to be a type then we should also be able to have variables of this type and indeed we can. So, I can declare a variable of type int star ok. So, note that this is. So, pointer a is still ptr a is still a local variable of main. So, there should be space allocated for ptr a in the activation record for main in the stack segment but ptr a is type is int star. So, it will basically contain the address of an integer variable. So, how much space should we need to store the address of an integer variable that depends on you know in your memory how many bits do you need to address a location. So, let us assume that all memory addresses are 32 bits right. So, how many addressable locations in memory does this give us using 32 bits we can have a total of 2 raise to 32 addresses each of the 32 bits could be 0 or 1 2 raise to 32 is actually a fairly large number. So, but however you could think of a computer which has even larger memory and you need 64 bits to address memory in that case every such pointer variable will also require 64 bits. So, the space required to store a pointer variable will depend on the actual number of bits used to represent an address in memory and for you know for our purposes we will just simplify life and say that there is just 32 bits 4 bytes. So, for this variable there must be space allocated in the stack segment because that is a local variable of main and that must be 32 bits of 4 bytes and of course, those memory locations will have their own addresses and so on just like any other variable is that clear. And now when I have an assignment like this pointer a is assigned ampersand a what am I saying I am saying look at the variable a find out its address. So, this was the variable a there were 4 locations for it. So, when I say find out its address which of those 4 addresses am I talking about there are 4 addresses. So, the convention will be that we will store the address of the first byte among the 4 bytes allocated for a. So, therefore, I will store hex 402 in the space allocated for pointer a. So, pointer a is space allocated are these green things or whatever whitish things and in that I will store the address of the first byte allocated for a. So, that is what pointer a is assigned ampersand a does right looks at the variable a find out its address of the first byte allocated for that variable and stores that in the space allocated for pointer a. So, now can we have pointers to pointers. So, it turns out that you know if you logically think about it it should be possible to do it. So, if pointer a is a variable of type int star what would ampersand pointer a be. So, it would be the pointer to a pointer to an integer right and so, basically the same thing if x is a variable of type t ampersand x is of type t star. So, ampersand pointer a would be of type int star star. So, this is how we represent a pointer to a pointer to an integer and how far can we take this as far as we want. So, for example, int with 4 stars is also a legitimate pointer type this is a pointer to a pointer to a pointer to an integer pointer. And this is note about declarations. So, often when we write programs we write things like int x, y, z instead of writing int x int y int z and that means all of x, y, z have the type int. But when I write something like this int star a, b star star c it is really you know. So, a is an integer pointer b is an integer and c is a pointer to a pointer to an integer right. So, it is not that both b and a are int star a is an int star I mean basically this star does not associate with the subsequent variables that you are declaring. So, it is int a is int star b is int and c is int star star. And then here is a simple program which has three variables of type int float and character and it has three pointer variables of type int star float star and cast star. And then you find out the addresses of these variables store them in the corresponding pointer variables and just print them out. So, has anybody tried to run this program? So, did you see the addresses coming out? So, that probably is the first time where you actually get a feel of what is happening inside the computer. I mean this is you are actually seeing the addresses of the memory locations that are being used for storing your. So, that is what this lecture was about. Are there any doubts? So, there was some discussion about the star operator we will just see that right now. So, once again this is the picture of memory, this is the picture of how memory is allocated. And now here is another program which has an integer variable which has some space allocated for it. There is an integer pointer variable space allocated for it and then some value is stored in the integer variable. So, what is going to happen that in the space allocated for that integer variable that value will be copied and then the address of that variable is stored in pointer a. So, the address of that variable is the address of the first location allocated for that variable. So, that is 6402 will be copied there. And now the question is that now that we know the address of the variable a can we access what is stored inside variable a just knowing its address without knowing its name. And that is what C plus plus allows using the star operator. So, if pointer a is a program variable of type int star then star of pointer a will give you the content of the memory location whose address is given by pointer a. So, pointer a is a variable of type int star. So, pointer a is storing the address of an integer variable. So, star pointer a will give you what is stored in that address. So, and then if I try to say something like you know I want to read out what is there in star pointer a. So, what this means is so, whenever you have this star you can think of it as saying the content of the memory location whose address is given by pointer a. So, pointer a is containing this address. So, the content of the memory location whose address is given by x 402. And now this comes to the question that somebody just asked from there that if I just ask you to find out the content of the memory location whose address is x 402 how do I know how many bytes to read starting from that location. For example, here this is an integer a is an integer and the starting location for that integer has the address 402. So, when I say star pointer a because I know that pointer a is an integer pointer. Therefore, I know when I talk about star pointer I have to go to that address and read 4 bytes. But if pointer a was a character pointer I would have to go to that location and read 1 byte. If pointer a was a double pointer I would have to go to that location and read 8 bytes. So, if you just give an address then you will also have to specify that address is an address of what is it an address of an integer is an address of a character it is an address of a double what and then you can put a star around it. So, if you just give a constant as an address then you have to specify that is that constant should be treated as the address of what type. So, this is usually what is called typecasting. So, you can take a constant address and you can say that this address should be treated as a double pointer and then you can do a star because then the computer can go to that address and now it knows how many bytes to read from there because that address was to be interpreted as a double pointer. So, but at least for this course we will I mean that is an interesting experiment for you to try out, but in this course we will not try to apply star on constant addresses we will try to apply star on legitimately formed addresses. So, this is what we just saw. So, when I say star pointer a I am saying find out the value of pointer a treat that as an address go to the memory location with that address and because pointer a is a pointer to an integer how do I know that that I know from this declaration over here pointer a is a pointer to an integer right. So, therefore, when I am trying to find the content of the memory location whose address is given by pointer a I know that I am trying to find out the content as an integer because pointer a is a pointer to an integer right. So, therefore, I will just go and read the 4 bytes and I will get x 0 to a dc. So, the unary star operator just like the unary ampersand operator it basically forms a C plus plus expression. So, you can say star of pointer a and the pointer is a pointer variable then this becomes a C plus plus expression and of course, if pointer a is int star star of pointer a is int right. If pointer a is int star basically pointer a is the address of an integer variable. So, star of pointer a will be the contents of that address that will be an integer and once again we will avoid worrying about operator precedence and associativity for star by just using parenthesis. So, this operation of applying star something which has the address of a memory location is also called dereferencing an address and you know now that we know how to dereference an address can we have dereferences of dereferences and you know I mean logically it should be possible to do this. So, basically here is a character variable here is a variable which is a character pointer here is a variable which is a pointer to a character pointer right and here I am. So, here is how the memory layout might look like. So, character variable requires just one byte to store a pointer to a character must store the address of a character variable. Now, addresses if once again we say require 32 bits then pointer C must store the address of a character variable. So, it will require 32 bits. So, it will require 4 bytes and pointer pointer C is the address of a character pointer variable right, but once again an address in memory is again going to require 32 bits if we assume that our memory has 32 bits of addresses and so that will also require 4 bytes right and then when I say pointer C is ampersand C the address of C will be stored in the space allocated for pointer C. So, x 302 will come there and then when I say pointer pointer C is ampersand of pointer C. So, the address of pointer C which is x 402 will be stored in the memory location allocated for pointer pointer C and finally, if I execute this statement saying read something and store it in C. So, it will actually get stored in the memory allocated for C right. So, let us say I have read some character whose ASCII code is x 30. I think this is capital A or something 48. And now if I say please print out star of star of pointer C. So, what am I asking I am saying look at the value of pointer pointer C that is x 402 applies star on it which means go to that memory address and read the contents of memory starting from that address, but how many bytes should we read pointer pointer C is a pointer to a character pointer right. So, when I go to that address x 402 and try to read off the contents from there I will be trying to read off the value of a character pointer. So, character pointer will require again 4 bytes any address requires 4 bytes. So, I have to read off 4 bytes and that will give me x 302 and then I have to apply the last star on it which is saying go to the memory address x 302 find out what is stored there and once again how many bytes should I read starting from that that depends on what star pointer pointer C is about. So, pointer pointer C is pointer to a character pointer. So, star pointer pointer C is a character pointer and therefore, when I go to x 302 I know that I am at a I am at I am basically at the address of a character and so I should just read off 1 byte. So, this will just explain in great detail there. So, you will finally read off this value right. So, what you see is that here I started off from this address from there I went to this memory location got the other address went to that memory location and I finally read that value. So, that is what the nested stars do go to an address read something again treat that as an address go to another address read something and continue like this. And we can certainly nest dereferences for example, if I have an integer if I have a variable x which is pointer to pointer to pointer to integer pointer all of these expressions are legitimate star x star x up to 4 star 6. But if you put 5 star 6 here that is not legitimate because when you dereference x 4 times in a nested way you actually get an integer. And so when you try to apply star on it you know it will say that you are trying to dereference an integer and it does not know it is a pointer to what right. And there are certain caveats that you should be careful about when using dereferencing. So, as I said you know right at the beginning that when a process is running when a program is running there is a certain part of the memory that is allocated to the program. This program is running there is a certain part of the memory allocated to this program. Now if you try to dereference an address which is outside this and of course it should not allow you to do that right because that is that is part of the memory that you should not be accessing. So, therefore you need to be careful that when you are dereferencing something you are actually dereferencing an address that is within the part of memory that you are allowed to access. Otherwise I am sure several of you have encountered error messages like segmentation fault or segmentation violation. So, that is basically a case where maybe you have an array of size 100 declared and you are trying to access the 200th element in the array. So, you have basically gone outside location that is allocated for you and that is not allowed. I mean you cannot access a location that is which does not have a legitimate address. And in particular the address 0 x 0 which means all 0 this address is also called the null address. So, this address is outside the memory space of every program of every program that is run. So, you should never try to dereference the address 0 this will certainly need to you know your program crashing same segmentation violation. So, you need to be careful that you are dereferencing addresses that are legitimate. So, that was that part any doubts? So, there were so you know so there was a question who asked the question about operator associativities yeah you asked the question. So, now if I say star of a plus 1 right. So, am I taking star of a. So, let us say a is an integer pointer. So, star of a will be an integer and should I add 1 to the integer values stored at the address given by a or should I take a as an address add 1 to it and then try to dereference that address right. So, depending on which operator has higher precedence you will have to right. So, but what we will do is and you know what is highly recommended is not to get into these you know tricky situations at all. So, just use parenthesis. If you wanted to do a plus 1 and then dereference it write a plus 1 within parenthesis and put the star outside. If you wanted to dereference a and then add 1 write star a within parenthesis and put the plus 1 outside. So, as operators you need to worry about these things, but I think it is best to just use parenthesis to not get into tricky situations ok. So, let us start the quiz. Let me move on to the second question. Hello everybody. So, this is a practice a question. So, you will be given around 2 to 3 minutes to find the answer to this. So, there are 3 things that you have to do. I think you are doing it every day that is a you are thinking for some time. So, maybe for 3 minutes or 4 minutes and then you are pairing with somebody. So, you are neighbors right. So, you are exchanging then discussing and then the third phase which is the sharing the when somebody is called to the board maybe writing you know or explaining the solution to that problem. So, all these 3 phases are important and it is not the only solution that you you get is important because you will get to learn more from your fellow students right. And also when somebody comes here explains right and it is also not important that you get an answer to the question, but if you understand the problem I think if you do not get the answer to the problem now maybe later on you will try to think about it, but if you do not understand the problem it will be difficult. So, you have to understand the problem. So, can you spend some 3 minutes on this. Now, you should be exchanging with your neighbors. Some people have understood that PTR is pointing to the first element of the array is it correct can anybody answer please. In this question first PTR equal to ampersand a of 0 it stores the address of the first element of array and then we enter the infinite follow the first statement sum equal to sum plus star PTR it will add the value of content of the address of the first element to sum that is 1 sum equal to sum plus 1. Now sum has the value 1 after this if star of PTR less than 10 which is true because star of PTR has the value 1 at present. So, now again we have pointer equal to ampersand a of star pointer PTR equal to ampersand a of star of pointer star pointer has the value 1 at present. So, now PTR gets modified to ampersand a of 1 now it stores the value of the second element of the array and so we progress again to the for loop again iteration takes place now sum equal to sum plus 2. So, sum equal to 1 plus 2 equal to 3 again if star PTR less than 10 is true because star PTR now has the value 2 and again it will continue so on until we reach 9. When we reach 9 sum equal to sum plus star PTR will be sum equal to sum plus 10 star PTR less than 10 is false hence it will break out of the loop in the process. So, we will add all the elements of the array 1 by 1 plus 2 plus 3 plus 4 and store it in sum. When PTR stores the address of the A9 that time it will break out. Thanks. So, we agree that whatever he has said I mean is correct right. So, I think many of you must have got the essence of this problem there we used parenthesis you know to explicitly state you know the address so that you do not have you know confusion. So, use parenthesis wherever required rather than not using it. So, let us go to the next practice problem which is almost same as the previous one, but I would like you to write it down the whole code because see this you can do it in your free time. Okay. So, the answer that I got I think is 10 and so first we know that sum is equal to 0 and we initialized PTR to have the address of A is with index 0. So, star PTR will have the value 9 and then we enter the for loop sum becomes equal to sum plus star PTR plus plus as we know sum is 0 star PTR has the value 9 and then you do the plus plus operation which increases it to 10 no with the brackets. So, yeah that is why I thought it was 10. According to me it's some star PTR plus plus so it's a post increment so the value of star PTR will change after this statement like for this statement it will be 9 only but after that the value of star PTR will be 10. Okay. So, what you are saying is that the star PTR is not directly star PTR equal to star PTR plus 1 hold on I will be finished. So, you are saying it's not equal to star PTR plus 1 but we got two sets of brackets. So, first do you agree with me that first star PTR is 9 that you agree with me. Okay. And then you are saying that 9 plus plus won't occur is that what you are saying? I am saying that star PTR plus plus that part would be 9 at that particular instant after that at that instant it would be 9 only the values stored there would be 9 but after that all values of star PTR would be 10. Okay. But then there is the second pair of brackets right. So, 9 will get incremented to 10 before the brackets open the second pair of brackets open don't you think so? I think the second pair of brackets won't make that bigger difference I feel the only star PTR like the first bracket has a greater significance to separate the star and the plus plus but the second bracket won't change the value to from 9 to 10. So, firstly you are saying that some will be 9 is that what you are saying? And then when do you think the for loop breaks? No, after this the value of star PTR would become 10 after this some step operation. So, when you go to if operator so, the condition is not satisfied. So, it will break. Okay. So, it will break immediately and I agree. See in that line now first PTR the value will be put 9 then in the memory location PTR the value will change to 10. Okay. Then I will in this statement I will use PTR as 9 only and fine. Yeah, he is right. I agree. Yeah, I got it. Thanks. So, does everybody agree to that? Yeah, you can look it I mean you can try and work out for yourself how it's going to work it's not that difficult if there is a problem I think you should type a program and see for yourself what output you get but that's not the only way to do. So, don't keep on typing the program if somebody gives you a program type C type C that's not the only way. But now in the beginning you can do that because you may want to understand something when you are typing when you are actually coding this try to change remove parentheses or something like that you know and see a different variation and see what really happens inside. So, then the thinking will happen right let's go to the third problem. So, you are not supposed to use a two-dimensional array. Okay. So, you are supposed to use a single-dimensional array and so as in as you would write say care care a of you know of size something right that's a declaration that you would have in your program. But while accessing you would like to access it as a 2d array. Okay. The problem I not get it. Oh, you are not getting still. So, the problem is you are not allowed to use a two-dimensional array directly by declaring it. When you say declare a two-dimensional array in your program you will say care a the first size and the second size which is a 10 10 you are not allowed to do that. So, you are allowed only one dimension which is a of 10 and then somewhere inside the program you want to access i and j that is a in a two-dimensional form a i and j. So, you start declaring as a single-dimensional array and then later on in your program you need to consider that as a two-dimensional array you access it in that fashion. So, is there a problem in understanding now? No, does anybody have a different problem than what we have stated or has a everybody understood? Would you please write the code for five minutes now or the intuition behind the code would be enough if you do not want to write the code the intuition behind how you would structure your code would be more than enough. Is that okay?