13.1 Introduction
The memory location where data is stored is called address. Normally the data or variables are accessed through names that represent them. For example, marks, age etc. Similar to data, code is also stored in some memory location and the same can be accessed through names that represent them for example, getch(), clrscr() etc. C++ allows data and code to be accessed through its address, rather than official name. This is done using pointers. Pointers are special variables that are used to store the address of data or code. It is one of the most exciting features of C++ and also needs great care while using this feature. The concept is called pointers.
13.2 Pointer
Pointer is a variable used to store the base address of another variable, reference or just a memory…show more content… The first line creates a variable with reference var. because it is of integer data type, 2 bytes of memory will be allocated to it in the memory and the value stored in it will be 10. You must be familiar with the next line that is used to display the value stored at the memory location referred by ‘var’ which is 10. The last line uses reference operator (&) in front of the variable name. It is used to display the memory location where the variable ‘var’ which is 104.
13.4 Declaring a pointer
A pointer in C++ is powerful enough to directly refer to the value stored at the location it points to. So, a pointer has different properties on the basis of data type it is pointing to. As you try to access the value of the address pointed by pointer, it is must to know its data type. Without data type, it is not possible to access it. Once de-referenced, the type needs to be known. That is why the declaration of the pointer also includes the data type.
A pointer can be declared as follows:
data_type *pointer_name; //pointer_name can be any legal identifier
For example, int *iptr; //pointer ‘iptr’ of integer data type has been