A computer’s 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 variables 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.
#include <iostream.h> void main(){ int x; cout <<"ADDRESS OF X IS:
"<<&x<<endl; x=5; cout<<"X IS:
"<<x<<endl; }//MAIN
Figure 13.1a – Program to show the address of a variable
ADDRESS
OF X IS: 0x74272400 X
IS: 5 Figure 13.1b – Output
of Figure 13.1a