Pointers in c++-: Pointer is one of the key aspects of c++ language similar to that of c programming. Pointer offers a unique approach to handle data in c and c++ language.
We know that a pointer is a derived data type that refers to another data variable by storing the variable memory address rather than data. a pointer variable define where to get the value of a specific data variable instead of defining actual data .
Like the c programming language variable can also refer another pointer in c++. However it often point to a data variable . Pointer also provide an alternative approach to access other data object.
Defining a pointer in c++ -: Like any other programming language before we use the pointer in c++ program it is necessary to defining. We can declare a pointer variable similar to the variable in c++. Like c language variable . The declaration is based on the data type of the variable it point to the declaration of a pointer variable takes like this
data-type * pointer-variable;
In this example the pointer variable is the name of the pointer and data type refer to the one of the valid c++ data type such as int,char,float and so many more. The data type is followed by an asterisk (*) which distinguishes a pointer variable from other variable to the computer.
As we know a pointer variable can point to any type of data available in c++. It is necessary to understand that a pointer is able to point to only one data type at the specific time. Let us declare a pointer variable which points to an integer variable as like
int *ptr ;
here ptr is a pointer variable and point to an integer data type. the pointer variable ptr should contain memory location of any integer variable. in the same manner we can declare pointer variable for other data type also.
Initializing pointer in c++ -: Like other programming language a variable must be initializing before using it in a c++ program. We can initializing a variable as like
int *ptr, a; //declaration ptr = &a; //initializing
in this example the pointer variable ptr contain the address of the variable a .
For better understanding we take a example of pointer
#include <iostream> #include <conio.h> void main() { int a, *ptr1, **ptr2; clrscr(); ptr1= &a; ptr2=&ptr1; cout << " the address of a :" << ptr1 << "\n"; cout << " the address of ptr1 :" << ptr2 ; cout << "\n\n"; cout << "after increment the address value :\n\n" ptr1+=2; cout << " the address of a :" << ptr1 <<"\n"; ptr2+=2; cout << " the address of ptr1 :" << ptr2 <<"\n"; }
Output of this program
The address of a : 0x8fb6ff4 The address of ptr1 : 0x8fb6ff2 After increment the address valu : The address of a : 0x8fb6ff8 The address of a :0x8fb6ff6
The pointer which are not initializing in a program called Null pointer.
Manipulation of pointer in c++ programming -: We can manipulate with the indirection operator as like “*” which is also known ad dereference operator. We can indirectly access the data variable content. For like this way
"pointer_variable"
We know that the dereference a pointer allows us to get the content of the memory location that the pointer points to after assigning address of the variable to a pointer we may want to change the content of the variable. Using the dereference operator we can change the content of memory location.
Pointer Expression and pointer arithmetic in c++ -: C++ allows pointer to perform the following arithmetic operator
- A pointer can be increment(++) and decrements (–)
- One pointer can be subtracted from another
- Any integer can be added to or subtracted from a pointer.
For example
int a[5]; int *aptr; aptr=&a[0];