 Hello and welcome back. In the last segment we discussed reference parameters. In this segment we are going to talk about pointers which do something similar. So if the memory of a computer has n bytes then you may recall that the bytes are numbered 0 through n minus 1 and this number of a byte is different from what is stored in the byte. So the number of the byte is said to be its address. So if you remember our analogy was the think of the bytes as sitting on a long street and you give them the number say left to right along the street. So from one end of the street to the other end of the street and each byte is given a number according to its position on the street. And of course in the byte you can store some other value, so that is different. So that is different from the address or the number that has been given which is the position of the byte along that street. So the number is called the address and of course we can store anything in every byte. The term pointer is used to denote a variable that can store an address. So an address is also a number but we are going to make a distinction because addresses are going to be used for very different purposes than ordinary numbers. And therefore we really do not want to mix up addresses and ordinary numerical values. So in any case you know that everything on a computer is a sequence of bits. So an address is also a sequence of bits, a number whether it is a floating point number or a fixed point integer is also a sequence of bits but we have to treat them differently. And so an address is a variable that can store addresses or sometimes you might use the term pointer to refer to addresses themselves. What we accomplished using reference variables can also be accomplished using pointers. So that is the motivation for studying pointers right now and pointers will also be useful elsewhere. So we will study that later. So we said that pointers are variables to store the address of a variable but let us now see how do we first get to the address of a variable. So the operator and can be used to get to the address of a variable. Now the same and is used to mark reference parameters but which one we are thinking of will become clear from the context. In fact if you remember we also had and and to denote the conjunction of two conditions. Again it is that same character but in this context we are using a single and and so do not confuse it with that and or either the and that is used to mark reference parameters. So here is a statement which creates a variable t in t. Now if I write c out and t then this is going to print out the address of t not what is contained what is stored in t but it is going to print out the address of t. So in particular so it is an int variable so you remember that an int variable takes 4 bytes so this is going to be the address of the very first byte amongst those 4 bytes. Now when you do printing like this and you can try it out customarily the radix used for printing addresses is the hexadecimal radix and in fact so your value will consist of a sequence of hexadecimal digits which are 0 through 9 and then a, b, c, d, e and f. And they are also prefixed by 0x 0x sort of stands for hexadecimal. So if you print this you will see some string looking like that and now we know how to print out addresses or how to get to addresses now we see how we are going to construct variables which can store addresses. So how do we create a variable for storing addresses of variables of type int? So this is how so we are going to write int star v yeah so as I just pronounced it this is going to be read or pronounced as int star v and of course again there is a confusing usage. The star when used in this manner is not to be read as multiplication. It is meant to be read as something else. So in fact we are going to think of the star as joining with int. So we can think of the entire int star as the type of v and you can think of int star together meaning address of int. So it is a value and the type of that value is address of int. So suppose we have int p defined later on. Now I can write v equal to address of p. So what does this do? So v is a variable which is supposed to contain addresses and p is an address of int this is supposed to contain addresses of int and and p is an address of int and therefore we can store such values inside v. So addresses can be stored, addresses of ints can be stored in v. So in fact c++ will check whether in fact the values that you are storing are of the right type otherwise the compiler will flag an error. Now if I print out v and I print out and p you will see that exactly the same value will get printed. So v now is the name of a variable but if I say print p the value of print pv what gets printed is its value. So v is not an integer. So the value that v contains is an address and so that gets printed. But here I am asking the address itself to get to be printed. So both will really print the same thing. Here is an interesting statement, what if I write v equal to p? Now v is a variable of type address of p but p is not an address of type address of int but p is not an address of int, p is an int. So c++ says that look both of them may be bit strings but I do not think you really want to do this. So there is no occasion where we really want to mix values and addresses and therefore c++ will flag it as an error during compilation. So in general we can create pointers to arbitrary types of variables not just ints maybe doubles maybe cars whatever we want. So to create a variable w to store addresses of variables of type t we will write t star w. So just as we wrote int star w we can write double star w, double star y or char star x whatever it is whatever we want. And assignment statements in general require the types of the left hand side of the assignment and the right hand side of the assignment to match. So if you create something which has type t star and you assign something to it then the value on the right hand side must also be of type t star. Now this type matching rule is not quite applicable if both sides you have numeric types. So we saw that earlier. So you can take a float value or a double value and store it into an int or vice versa. But that is an exception made to the very general rule of type matching on both sides of the assignment statement when both sides are numeric. But if both sides are not numeric then there is no automatic conversion made from one type to another. And in fact the compiler will flag an error. So there is no conversion rule between pointers of one type to values of another type or pointers of one type and pointers of other types. So we have the address of operator and we also have the dereferencing operator. And the dereferencing operator is a star. So again they should not be confused with multiplication. So if V contains the address of P then we can get to P by writing star V. So here is an example. So int star V declares V to be a variable of type address of int. Then int P says P is just a variable of type int. Then we can write the assignment V equal to address of P. So V now contains the address of P. And now if I write star V, so star V is the dereferencing operator. What that says is this entire expression should be thought of as referring to the variable whose address is contained in P. Well, so which variable's address is contained in P? P itself. In V the address of P. And therefore star V should be thought of as P. And therefore this entire statement should be thought of as P equal to 10. So these stars and ants are really very simple, very straightforward. But they are not the same, they are in fact inverses of each other. So one says, so ant says give me the address of this variable. Star says give me the variable whose address is in this variable. So ant P is the address of variable P and star V is the variable whose address is in V. So this gives us a slightly different way of thinking about the definition of V. So the definition of V is int star V. So sometime ago we said that we can think of this definition as V is an address of an int. So this is what we said earlier, the second line. V is an address of int because we think of int star as an atomic term and int star we think of as an address of int. So here we are declaring V to be address of int. But there is a different way of thinking about this as well which says that this is not really a declaration of V directly. It is a declaration of what is contained in V. So what is contained in V, ints and therefore V is of type address of int. Perhaps the more accurate way of viewing this is that this is a declaration of star V or whatever is contained inside V. So sometimes this view is a little bit more important and we will see a use for that. But both are sort of good but this view that the first view written over here is slightly better for a reason. So now we are going to see a use of pointers when we write functions. So we are going to write the function which converts Cartesian to polar. So it is going to receive the Cartesian coordinates by value in these parameters x and y. But in addition it is going to have two parameters P r and P theta. So P r has type double star or it is an address of a double value and similarly P theta is the address of a double value. So let me just write down the function first and then I will explain. So what happens over here? So the main program contains this statement which has address of r being passed and address of theta also being passed. So what is this? So first of all C++ requires that whatever type you have over here had better match the type of the parameter over here. So what is the type of this parameter P r? So it is address of double. That is what this double star says that P r has type address of double and this in fact is address of double. So this type matches. This says P theta is address of double and in fact and theta is the address of theta which is a double variable. So the pointers are types of the arguments are matching. So what happens? So the types are matching. So the call will copy the values 1 into x and 1 into y but it will copy the address of r into P r and address of theta into P theta. So now something interesting has happened. C to P which is executing in its own activation frame has received addresses of variables defined in some other activation frame. In the activation frame associated with the main program. So when I execute this, what is going to happen? So what does this mean? The rule is as before. So star PR means that variable whose address is in PR. Now when I do this, this variable is not living in the activation frame of C to P. But I have its address and I can dereference. I can use the dereferencing operator and therefore this entire expression really now will mean the r variable which is appearing in the main program, in the activation frame of the main program. So it will refer to this variable. So whatever value we calculate over here will get stored, actually stored in this variable of the main program. Similarly P theta is the address of some variable. Well it is the address of this variable theta because that is what we passed over here. And when we do star of P theta, this expression is the same as this variable. And therefore 8 and 2 of x, 5 is going to get stored in this variable theta. So as a result, so in this case 1, 1, so square root of 2 in R because of this statement. And 8 and 2 is going to be placed in theta because of this statement. So when the call finishes, what you are going to get is 1 so is going to get square root 2 over here and pi by 4 over here or 45 degrees over here as we got when we used reference parameters. So again C to P is going to change the variables of the main program. But the mechanism is slightly different. Here we have been, we have sent them the addresses and there we sort of say that look you treat your R and theta to mean my variables. Whereas over here we have actually sent, we have actually sent them the addresses. It is like saying to somebody look I am not going to send you the value, I am not going to send you the money. I am going to send you the address of the bank from where you can get the money. Or I am going to send you the address of the bank where you can deposit the money. So that is what is happening in this function and that is what pointers enable you to do. So this will exactly do what we wanted. So there is a remark to be made which is that star in definitions has a slightly unexpected behavior. So the star actually associates to the right. So suppose I write int star v, p. I am inclined to read this as that the star sort of I read it as int star and then v is int star and p is also int star. That would be one way C++ could have defined this but that is not how C++ has defined it. And the definition that C++ has is perhaps slightly unfortunate but it is there. So what is the definition? So it says that we think of this as is the type of the variable which are contained in v. Not the values that are contained in v but it is the type of the variables that is contained in v. So basically it does say that v contains addresses of int variables but it is saying it in a roundabout way. So int is this whole thing is an int and similarly so when I write this this p is an int. So this means int star v and int p not int star v and int star p because this star sort of binds to v and not to int. And for now assume that the only operations you can perform in a variable of type t star or an address of something or that you can dereference it. So I can write star of v if I have a variable of type address of int or I can store into it value address of v. So you saw an example of this earlier. So I can have int star v and int p and I can say v equal to address of p. So this is a variable of type int star and what I can do with it is I can store a value into it which is address of some int variable. So this is what I can do with it or I can say c out star v or I can say star v plus plus. What would this do? If v has been said to be address of p this is going to be the same thing as p plus plus. I can do one more thing. I might have int star w in addition to int star v. So in that case I can write w equal to v. So whatever variables address was in v now is also stored in w. So this is completely consistent but you may find it unusual simply because you are not accustomed to dealing with pointers and so addresses and dereferencing addresses. But think of it one line at a time and it should be quite clear to you. So look at every expression, look at what the definition of that is and if you do this a few times it will become second nature. You can pass it to a function as an argument provided the corresponding parameter is of the right type that is t star. So if you have a variable of type t star then you can pass it as a parameter to a function in which as an argument to a function if the corresponding parameter is of type t star. So now here are some exercises for you. You have to point out the errors in this code and you are also to tell what gets printed as a result of executing this code. So what did we discuss in this segment? So we said that a pointer is an address or a variable containing an address. A pointer to a variable can be created in the main program and dereferenced during a function call. So this way a function can be allowed to modify variables in the main program or variables in other functions as well. However we wherever we create the pointers and if we pass it pass to other functions. Pointers in some sense are doing the same thing as references but the notation is slightly more I mean the is clumsier in the sense that you have to explicitly say that now dereference it. Whereas in the case of a reference you declared at the beginning that oh this name is just going to mean this other name. Whereas here that name does not really mean the other name this name is means the address of this name and then you have to say that okay I want to get to the name the variable itself whose address I have and therefore you have to use that dereferencing operator. So pointers can do what references can do but they can do other things as well and we will see all of that later. So we will take a break here.