| INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS |
ADDRESS, POINTER VARIABLE,
DYNAMIC MEMORY ALLOCATION
A computers memory is divided into slots. Each slot has an address that can hold a value. A memory that holds a value and has the ability to change it when necessary is known as a variable. There are variables that hold values such as integers, characters, or other data types. A variable that holds an address of another variable is known as a pointer variable or simply as a pointer. Pointers provide open access to memory addresses and are beneficial to programmers, but pointers can put the security of a system at risk. For this reason, pointers are a controversial topic. The storage for pointer variables can be allocated during run time (dynamic allocation), as the program requires the memory. The memory for a dynamically allocated variable can be freed after its usage. This allows availability of the storage for other variables. The allocation and de-allocation of memory leads to a great saving of computer memory and a programmer can build their own data structures and manipulate them as desired. Furthermore, this provides other alternatives in building data structures.
ADDRESS OF A VARIABLE
Every variable has an address and a value. Normally when a variable is declared, an address is assigned to the variable by the system (compiler). The address of a variable is accessed by an ampersand &; it is known as an address operator.


![]()
A POINTER VARIABLE
A regular variable holds a value such as an integer. A pointer variable, or simply a pointer, holds a value as well but its value is the address of another variable. Through this address, the value of the variable can be accessed. In other words, a pointer can indirectly access the value of the variable. An asterisk (*) before the variable name is used to declare a pointer variable.


![]()
DECLARING A POINTER VARIABLE
The method used to declare a pointer variable is similar to an ordinary variable except that an asterisk (*) is placed before the variable name. A pointer variable can be declared of a primitive type such as int, char, float, double, as well as other structured types such as an array, structure, or class. In the following example, x is a pointer variable that points to an integer value and, similarly, y is a pointer variable that points to a character value.

![]()

![]()
ACCESSING THE CONTENT OF WHAT A POINTER POINTS TO: INDIRECTION OPERATOR
Accessing the content of what a pointer points to is similar to the way the value of a regular variable is accessed with one exception: the usage of an asterisk before the pointer variable. This indirect access of the variable value is known as indirection and the asterisk (*) before the variable name is known as the indirection operator or de-reference operator.