In order to have a pointer in fact point to one more variable it is essential to contain the memory address of that variable as well. To acquire the memory address of a variable (its location in memory), put the & sign in frontage of the variable given name. This makes it provide its address. This is called the address-of operator, as it returns the memory address. Conveniently, both ampersand as well as address-of start with a; that's a helpful means to keep in mind that you employ & to acquire the address of a variable.

For example:

Code:
#include <iostream>

using namespace std;

int main()
{ 
  int x;            // A normal integer
  int *p;           // A pointer to an integer

  p = &x;           // Read it, "assign the address of x to p"
  cin>> x;          // Put a value in x, we could also use *p here
  cin.ignore();
  cout<< *p <<"\n"; // Note the use of the * to get the value
  cin.get();
}
The cout outputs the value stored in x. Why is so as to? Well, let's glance at the code. The integer is called x. A pointer to an integer is then definite as p. Then it stores the memory place of x in pointer by means of the address-of operator (&) to acquire the address of the variable. By means of the ampersand is a bit like looking at the tag on the security deposit box to observe its number somewhat than looking inside the box, to get what it stores. The user then inputs a number that is stored in the variable x; remember, this is the same location that is pointed to by p.

The next line then passes *p into cout. *p executes the "dereferencing" operation on p; it looks at the address stored in p, as well as goes to that address plus returns the value. This is similar to looking within a secured deposit box only to locate the number of (and, presumably, the key to) one more box, which you then open.

Notice that in the above illustration, pointer is initialized to spot to an exact memory address earlier than it is used. If this was not the case, it might be pointing to something. This can lead to very disagreeable effects to the program. Such as, the OS will most likely avoid you from accessing memory that it knows your program doesn't own: this will cause your program to stop working. To keep away from crashing your program, you should forever initialize pointers before you make use of them.

It is also feasible to initialize pointers by means of free memory. This enables dynamic part of array memory. It is most helpful for setting up structures called associated lists. This complex topic is also multifaceted for this text. An understanding of the keywords new as well as delete will, but, be extremely helpful in the future.

The keyword new is used to initialize pointers with memory from free store (a section of memory accessible to all programs). The syntax looks like the example as shown below:

Code:
int *ptr = new int;
It initializes ptr to point to a memory address of size int (because variables have unusual sizes, number of bytes, this is essential). The memory that is pointed to become unavailable to additional programs. This means that the vigilant coder must free this memory at the conclusion of its usage.

The delete operator frees up the memory owed through new. To do so, the syntax is as in the example.

Code:
delete ptr;
After eliminating a pointer, it is an excellent suggestion to reorganize it to point to 0. When 0 is allocated to a pointer, the pointer becomes a null pointer, in further words, it points to not anything. By doing this, when you do somewhat stupid with the pointer (it happens a lot, even with qualified programmers), you discover out directly instead of later on, when you have completed considerable damage.

In actual fact, the perception of the null pointer is normally used as a means of representing a problem-- such as, several functions left over from C return 0 if they cannot properly assign memory (notably, the malloc function). You want to be sure to handle this properly if you ever make use of malloc or else other C functions that return a "NULL pointer" on failure.

In C++, if a call to new fail as the system is out of memory, then it will "throw an exception". For the time being, you need not be anxious too much regarding this situation, but you can read more concerning what happens when new fails.