 Hello, and welcome back. This is the second part of our two part lecture on introduction to pointers in C++. Here is a quick recap of some of the relevant topics we have already studied. We have looked at basic programming constructs in C++. We have looked at the pointer data type in C++. We have looked at the address of or ampersand operator in the C++ language. And we have also looked at some caveats that we need to be careful about when using the address of operator. In this lecture, we are going to find out how to find the content at a given memory address. This is also called dereferencing a memory address. We are going to also look at the contents of operator in the C++ programming language. Now, let us quickly look at our view of memory and addresses of locations in memory. So, basically a memory is a sequence of physical storage locations and each location stores exactly one byte or 8 bits of information which are also called the content or value of that location. And each physical memory location also has a unique address by which it is identified. And this is like the sequence, this is like the index of that memory location in the entire sequence of storage locations. So, for example, this memory location has the address 40a in hexadecimal. Usually in C++, when we try to print out addresses, it gets printed in hexadecimal. So, therefore, I am using hexadecimal to denote addresses of memory locations and 1111000 is the content of this memory location. This is its address and these are the contents. We have also seen that when a program is executing which is what we also called a process, then the operating system allocates a part of main memory for use by the process. And this part of the memory allocated for the process is actually divided into three segments, the code segment, data segment and the stack segment. And we have seen in an earlier lecture that all local variables of all functions in a program are basically allocated space in the stack segment. So, let us look at a simple C++ program where we will try to access the content at a given memory address. So, let us start off in this way. We have an integer variable a. We know that an integer requires 4 bytes for its storage. Each memory location stores just 1 byte. So, we are going to allocate 4 consecutive memory locations to store the value of a. Let us say for the time being these are the 4 consecutive memory addresses for these 4 locations. Pointer a is a pointer type variable. It is basically a pointer to an integer. Let us assume that every memory address can be represented in 32 bits or 4 bytes. So, pointer a being a pointer type variable it stores an address. So, it requires 32 bits. So, we are going to allocate 4 locations or 4 bytes in the stack segment for this variable. And let us say these are the 4 consecutive addresses of those 4 locations. Now, when I execute the statement a assigned hex 0 to a b c what is going to happen in the memory locations reserved for the variable a this value is going to get copied. So, that is what I will see over there. After that when I try to execute this statement pointer a is assigned ampersand a what is going to happen. I am first going to find out the address of the memory locations corresponding to the variable a. Now, there are 4 memory locations corresponding to variable a as we have seen in the earlier lecture that we are going to basically pick up the address of the first of these 4 consecutive memory locations. So, we are going to pick up hex 0 0 4 0 2 and store it in whatever part of the memory was reserved for the variable pointer a. So, that is what is going to happen over there. Now, the question is given that the variable pointer a is storing the address of the first of 4 locations reserved in memory for the variable a given that I know this address can I actually find out the value of a in memory. Now, it turns out that in C++ we can do this by means of a special operator called content of operator. In your program this will be denoted by the star symbol and unlike the multiplication operator which is also denoted by the star symbol and which is a binary operator a multiplication operator takes 2 operands. Here the content of operator which is also represented by star will be a unary operator it will take exactly one operand. So, here is how it works suppose pointer a is a program variable which is basically the address of another integer variable. So, it is type is in star it is a pointer to an integer if so then star pointer a will give the value of the integer stored in that variable to which pointer a is pointing. So, basically when I write star pointer a I am saying pick up the address given by the value of pointer a go to that memory location corresponding to that address and find the value of the integer stored in that memory location. Let us see how this works in a real C++ program. So, here is our previous program, but now we have augmented it by this statement where I am trying to print out the value of star pointer a this is the unary star operator the content of operator. So, how do we go about doing this? I am basically asking let us find the content or value at the address given by the value of pointer a. So, we are going to find the value of pointer a treat that as a memory address we are going to go to that memory address and find out what value is stored there and then we are going to print that out well. So, the value of pointer a is hex 00402. So, you have to find the content or value at this address how do we do that we have to basically read bytes starting from the address hex 00402, but how many bytes should we read? Now, in order to answer that question we have to go and figure out what the type of this variable pointer a was. Now, we know that pointer a was basically a pointer to an integer. So, it was storing the address of an integer. Therefore, when I pick up the address from the value of pointer a and try to go and access that memory location I am trying to read an integer and since an integer requires 4 bytes I must read 4 bytes starting from the location hex 00402 which is the address given by the value of pointer a. So, this statement what it does is that it reads 4 bytes starting from the memory location at address hex 00402. So, basically it reads hex 02 a b c and then it prints it out. So, that is what the star operator does it gives the content at the address given by whatever value pointer a has. As I said it is a unary operator it takes a single argument and if pointer a is a pointer type variable then star pointer a the content of pointer a is a c plus plus expression. Now, because this is an operator operating on a variable to give you an expression we do need to worry about operator precedence associativity and all of that, but we will simplify our lives and we will use parentheses to clearly denote the precedence and associativity when we are using the star operator. Now, this process of accessing the content at a given memory address is also called dereferencing of an address and it is interesting to note that we can actually have spaces after the ampersand operator recall this is the address of operator that we studied in the last lecture and the star operator which is the content of operator. So, when we are applying the address of operator or the content of operator to a variable we can actually use spaces between the operator and the variable, but we need to use these carefully basically I could write ampersand a or ampersand space a star pointer a or star space pointer a now we should use these spaces carefully to make our program more understandable more readable. So, if in the program context putting a space makes it more readable feel free to use it if not putting the space makes the meaning of the program more clear it is ok not to use that space. Now, we have seen how to dereference a memory address can we have dereferences of dereferences and in fact it is possible to do this in c plus plus. So, here is a little c plus plus program where I have a character variable which has been allocated a byte and that is its memory address then I have a pointer to a character variable and since a pointer must store an address. So, 4 bytes have been allocated assuming all addresses at 32 bits requiring 4 bytes and these are the corresponding memory addresses for pointer c pointer c is a pointer to a character pointer. And since it is a pointer therefore, it is an address it will require 4 bytes and these 4 bytes are allocated with these corresponding addresses for pointer pointer c. Now, when I execute this statement I am saying find the address of the variable c and store it in the memory locations reserved for pointer c. So, the address of the byte reserved for variable c is 0 0 3 0 2. So, I will copy hex 0 0 3 0 2 in the bytes reserved for the variable pointer c. The next statement says find the address of pointer c and store it in the space allocated for pointer pointer c. Now, we have seen earlier that when I apply the ampersand operator and when multiple bytes are allocated for a variable we pick up the address of the first byte. So, in this case I will pick up hex 0 0 4 0 2 and I will store it in the space allocated for pointer pointer c. Then I am asking for c to be read from the input let us say we read in a character whose ASCII code corresponds to hex 0 0 0 3 0 this is the ASCII code 48 because this is in the hexadecimal notation. And now we want to print out star star pointer pointer c note that I have used parentheses to clearly denote what am I dereferencing at any point of time. So, what is the value of star pointer pointer c to begin with? Well pointer pointer c has the value hex 0 0 4 0 2. So, we have to go to the memory location hex 0 0 4 0 2 and read off some bytes. So, I have to find the content at address hex 0 0 4 0 2, but how many bytes should I read from there? In order to answer that question we have to go back and ask what is the type of pointer c? Basically it is a pointer to what? Now pointer pointer c we know from this declaration is a pointer to a character pointer. Therefore, it is basically pointing to a character pointer and a character pointer like any other pointer is an address if all memory addresses can be stored in 32 bits a character pointer would require 32 bits of 4 bytes. So, when I do star pointer pointer c, I will have to find the value of pointer pointer c go to that memory location and because pointer pointer c is a pointer to an address therefore, I have to read the 4 bytes to get the address. So, therefore, I will read the 4 bytes and I will get the value hex 0 0 3 0 2. Now, once that I have obtained that star pointer pointer c is hex 0 0 3 0 2 what is the content of the memory at address hex 0 0 3 0 2. Once again I will have to go to that memory location and how many bytes should I read now? I will have to go back and ask what is the type of star pointer pointer c? Pointer pointer c is a pointer to a character pointer. So, star pointer pointer c must be a character pointer. Therefore, when I am dereferencing star pointer pointer c, I am basically trying to read a character. So, character requires only 1 byte and therefore, I must read 1 byte starting from location hex 0 0 3 0 2 and that gives me the value hex 0 0 0 3 0 that is what is going to be printed out over here. Now, how far can we nest dereferences as far as you want? For example, if x is a variable of type in star star star star star, then we can use up to 4 levels of dereferencing of x. Basically, a variable x of type in star star star star is a pointer to a pointer to a pointer to a pointer to an integer. So, if I write star x, I can basically think of separating out this last star from here and keeping the remaining 3 stars within. So, star x is basically an expression of type in star star star. Similarly, star star x I have separated out 2 stars here kept 2 stars back within. So, star star x is an expression of type in star star and so on. Finally, star star star star x is an expression of type int. What happens if I try to dereference star star star x? Remember star star star star star x is an integer not a pointer. So, I cannot legitimately dereference it. So, the compiler will report an error. Now, there are certain caveats when dereferencing there are certain memory addresses which are actually outside the part of memory allocated to the to a process by an operating system. If you try to dereference an address outside the part that is allocated to you, it will give a runtime error. You will see error messages like segmentation violation or program crashed. Particularly, the address 0 is never within any user processes memory space. Therefore, if you try to dereference 0 in your program, you are certain to get a segmentation violation and your program is certain to crash. So, you need to be careful when you are dereferencing that you are indeed looking at the contents of memory within the part of the memory that is allocated to you. Your addresses should be within the address space that the operating system has allocated to you. So, in summary in this lecture, we looked at how to dereference a memory address which is basically how to find the content at a given memory address. We looked at the content of operator and some caveats when using this operator and now from what we have learnt in the last two lectures, we can access memory locations through their addresses. Thank you.