POINTER

A pointer is a variable that can store a memory adddress. You use a pointer variable and the memory address stored in that pointer variable to indirectly access the value stored at a memory address.

USING POINTER

The primary reason  for using a pointer is that it knows where a value is stored in memory. The advantages of using pointers are:

ADDRESS OPERATOR

The C++ address operartor (&) is used to obtain the memory address that is associated with a variable. The address operator &, when followed by a variable name, gives the address of that variable. All variables are stored in memory at some addresses. When C++ executes the statement int x = 10, it will allocate the memory to store the value x. The diagram below shows the representation  of the value in memory.

200

10

202  

The value of the variable is stored in the memory location   200. The variable x has a memory address 200. It takes two bytes to store an integer data type. The next memory location available to store something else is at the location 202. The program below prints the value of the variable x as well as the memory address associated with the variable x.

//Declares and initialize a variable x
int x = 10;
//Display the value of x
cout << x <<endl; 
//Display the memory address associated with x
cout <<  &x << endl;

Output

10
200

 

POINTER DECLARATION

Pointer holds an memory address. The memory adddress is always a  long integer data type. Pointer, like any variable, must be declared before they can be used. The declaration

data type   *  pointer_variable;

where

Example Comments
int  * ptr_x; Declare a pointer variable ptr_x. Let's say the ptr_x has a value of 200 (the memory address 200), the value stored in that location must an integer data type.
Example Comments
float   *  yPtr; Declare a pointer variable yPtr. Let's say the yPtr has a value of 125 (the memory address 125), the value stored in that location must an float data type.

POINTER DECLARATION AND INITIALIZATION   AND INDIRECTION OPERATOR

Declare and initialize a pointer variable

                             data   type   * pointer_variable  =  &variable;
or
data type * pointer_variable = array_name;
or
data type * pointer_variable = another_pointer_variable;

Indirection Operator

The * symbol also serves as the indirection operator, which indirectly accesses the value to which the pointer points.

Example:

//Declare a variable x.
int  x  = 35;
Assume C ++ allocates the memory location 100 to store the value of  x  (35).
//Declare and initialize a pointer variable ptr_y.
int  *  ptr_y =  &x;
The pointer variable ptr_y holds the memory address of  x  (100). Assume C++ allocates the  memory location 400 to store the memory address 100.
As the figure below shows, the ptr_y now points to the integer stored in the memory address (100) associated with x  and the pointer  ptr_y is stored at memory location 400. The next available memory location after 400 is 404 because the pointer variable holds a memory address which is always a long integer data type (4 bytes).

Variables Memory
Location

Value

x 100 35
ptr_y 400 100


//Display the value of the variable x
cout<< x << endl;  // 35
//Display the memory address of  x
cout << &x << endl; // 100
//Display the memory address of y
cout << &ptr_y << endl; // 400
//Access the value to which the pointer variable ptr_y  points to
cout << *ptr_y << endl; // 35
//Increment the variable x to 10
x = x +10; // x = 35 + 10 =45
Now the memory location contains a value of 45.

Variables Memory
Location

Value

x 100 45
ptr_y 400 100

//Display the value of the variable x
cout<< x << endl;  // 45
//Access the value to which the pointer variable ptr_y points
cout << *ptr_y << endl,  // 45
//Increase the value to which the pointer variable ptr_y points to 50
*ptr_y = *ptr_y  +  50; // *ptr_y = 45 + 50 = 95
Now the memory location 100 contains a value of 95

Variables Memory
Location

Value

x 100 95
ptr_y 400 100

//Display the value of the variable x
cout << x << endl;  // 95
//Access the value to which the pointer variable ptr_ y points
cout << &ptr_y << endl;  /95

POINTER ARITHMETIC

Pointer arithmetic allows you to add to pointers, subtract from poiners, compare pointers, and subtract one  pointer from another. You must declare a pointer to point to the correct data type if pointer arithmetic is to work correctly.
When you add an integer to a pointer, the actual value added is the number of bytes or the size of the data type to which the pointer points. The example below illustrates the  use of pointer arithmetic.
Example:

//Declare and initialize a variable x,y,z and v
int  x   = 10;  int  y = 30; int  z  =  50; int v = 60;
//Declare and initialize a pointer variable ptr
int  * ptr = &x;
Assume C++ allocates the memory locations 100, 102, 104 and 106 to store values of x, y, z and v respectively, and the memory location 300 to store the pointer variable ptr. 

Variables Memory
Location

Value

x 100 10
y 102 30
z 104 50
v 106 60
ptr 300 100

//Display the value of x
cout << x << endl; // 10
//Access the value to which the pointer ptr points
cout << *ptr << endl;  // 10
//Add 1  to the value of ptr
ptr++; // ptr = ptr +1 = 100 +  2 (the size of integer data type is 2) = 102
Now the memory location 300 has a value of 102

Variables Memory
Location

Value

x 100 10
y 102 30
z 104 50
v 106 60
ptr 300 102

//Access the value to which the pointer ptr points
cout << *ptr << endl; // 30
//Add 2 to the value of ptr
ptr = ptr + 2; //  ptr = 102 + 2 = 102 + 4 (2 * size of integer data type) = 106
//Access the value to which the pointer ptr points
cout << *ptr << endl; // 60