With our address book analogy, you now know of most of the operations we can do on pointers. In this episode, we will go over all these operations one after the other and discuss how to use them in C++ language syntax. To declare a variable in C++, we know we can use the following syntax. So, name of the type, for example "int" for integers followed by the identifier associated to the variable, i.e "x". For a pointer, the notation is the same, except that the type will indicate that it is a pointer to a particular data type. So for example if I want to declare a pointer on an integer, the associated type will be: "int*" to mean "pointer to an integer", followed by the name of the pointer, so of the pointer-type variable we are creating. Similarly, if I want to declare a pointer on a "double" I will use the following syntax. I will write "double*" as the type followed by the identifier for the variable, "ptr". So here, I declared a variable named "ptr", the type of which is pointer on a variable of type "double". This is for simple declaration. If I want to initialize at the same time, the syntax is analogous to the one I used up until now. For example, if I want to write, declare and initialize a variable "x" of integer type, initialized to 4, I use the following syntax. Well, the syntax is quite analogous for pointers. I must indicate in parentheses the value of the pointer. And this value must be an address. So here are three examples of declaration-initialization situations on pointer-type variables. Here, we are taking a bit of ahead start on what will follow in this episode and the following ones, by seeing how to obtain the address of a given memory area, in this case the address of the memory area named "i", the variable "i"; and how to dynamically allocate a memory area capable of storing data of a certain type, in this case an integer. So of course, we will review these concepts in much further detail when the time comes. First line here, I am declaring-initializing a "ptr" variable of type pointer to integer So the "ptr" variable can contain the address of an integer and we initialize this variable using a special value which is "nullptr" meaning that the "ptr" pointer points to nothing. So remember our address book analogy. Here, we have a page of our address book which is empty, which is initialized to nothing, which contains nothing. Second instruction, I initialize a "ptr" pointer using the address of an existing memory area, of a variable which exists in memory. We will come back to this later. And here, I initialize my pointer using the address of a dynamically allocated memory area. These concepts will be the focus of the following episodes and of the rest of this sequence. By the way, please note that the special value "nullptr" can be assigned to any type of pointer -- be it a pointer to a "double", pointer to an integer, pointer to any other type of data defined in C++. In either case, it means that the pointer points to nothing. C++ provides two fundamental operators for pointer manipulation. The '&' operator (ampersand), and the '*' operator (star). The role of the '&' operator is to return the memory address of the value of a variable. Let's see what this means on a real example. Here, we have a small program which starts by declaring a variable 'x' of integer type, and initializes it to 3. We know that to each variable of a program, an address in memory is assigned. Let's name this address "adr". Then, we declare a second variable "px" which is of type pointer-to-integer. Meaning that it is capable of containing the address of a memory area containing an integer. So we have this situation. And here, we will start by initializing the variable "px" using the special value "nullptr". Which basically means that "px" is capable of pointing to a memory area containing an integer, but that at this moment, it points towards nothing. It points to nothing at all. Now, the third line of the program. And this is where our "&" operator comes in. We assign, to the variable "px", the address of the variable "x". We have seen that this address here is "adr", so this is what we end up with. We will assign the address of the variable "x" to "px". We are establishing the link between pointer and pointed-to area with this assignment. So to sum up, "px" returns the address associated to the variable "x". The '*' operator allows us to get the value pointed to by a pointer-type variable. Earlier, we learned about the '&' operator. So "&x", where "x" is a variable, will return the address of "x" in memory. Now, imagine that we have a pointer "px". This pointer points to, contains the address of, a memory area containing a certain value, for example 3. And we wish to find the value pointed to by using the pointer. For this, we need to use the '*' operator applied to a pointer-type variable. This is what will allow us to get the value pointed to by this pointer. So if we go over our small example from the previous episode again, we go through the first instructions and we find ourselves in the following situation. A variable "x" initialized to a value of 3, of integer type and which has an address. A second variable "px" which is of pointer-to-integer type which we begin by initializing to "nullptr", meaning that it points to nothing. And to which we then assign the address of "x" which will establish the link between pointer and pointed-to variable. Then, last instruction, and this is where we call our famous little '*' operator which will allow us to establish the link between pointer and pointed-to value. So here, we have "*px". we use the '*' operator on the pointer, which will allow us to get, through the pointer, the value pointed to by this pointer. Now, imagine that I have a variable of type "int" named "i" which contains a certain value and which has an address in memory. If I write "&i", I will get a value of type pointer-to-int which is the address "adr". Now, if I write " *&i ", I will apply the '*' operator to this pointer which will return the value stored at that address, which is 3. So writing " *&i " is exactly equivalent to writing "i" since I directly access the value contained in "i". C++ has the reputation of being a language with a somewhat complicated syntax. The '*' and '&' operators which we have just seen are a first example of that. In fact, depending on the context in which they are used, these operators do not necessarily always mean the same thing. Let's see why. There are now two contexts in which we use the '&' operator. The first context is when the ampersand follows a type name and the second is when the & precedes the name of a pointer-type variable. In the first case, we will use the '&' to declare a reference, either when we pass a variable by reference to a function, or when we declare a reference on its own. For example, I can declare a variable "i" of integer type and I can declare a reference to this variable named "id". This simply means that "id" is now another name for the variable "i". So, first context of use, we use the "&" to indicate that we are declaring a reference. Second context of use: when the ampersand symbol precedes a variable name. We have just seen that in this case, we will simply obtain the address of this variable. And this address can of course be assigned to a pointer-type variable to establish the link between a pointer and a pointed-to variable. So here, we have a context of use which is completely different from the first and which leads us to a totally different situation. So, first potential for confusion: two possible contexts for use of the ampersand which means different things in C++. Second example where C++ uses the same symbol to represent two different things: the star '*'. Indeed, we saw earlier that when a '*' follows a type name as it is the case here, it is simply used to declare a variable of pointer type. For example, if I write something like this in a program, I am declaring a variable named "id" and this variable is defined as having a pointer-to-int type thanks to this '*'. This means that it can store, potentially, the address of a memory area containing an integer. Like so. So "id" is a variable of pointer type declared using '*'. If, however, the '*' precedes the name of a variable, this supposes that the variable considered is a pointer-type variable. And the '*' will serve to return the value pointed to by the pointer. And here, this is the situation we are in. We have a pointer-type variable which contains the address of a certain memory area, which contains a value. Let's say that this variable is an integer. So "*id" will simply return the value of 3, the value pointed to by the "id" pointer. So here, the '*' serves to find the value pointed to by a pointer. It is a context of use which is quite different from what we have seen up until now.