Pointer in C++, Pointer declaration, Pointer operator, Address operator,Constant pointer, Void pointer, Null pointer, Pointer Arithmetic in C++

Explain Pointer in C++, Pointer declaration, Pointer operator, Address operator,Constant pointer, Void pointer, Null pointer, Pointer Arithmetic in C++

Pointer in C++



A pointer is a variable which holds the address of any variable or a function. A pointer can refer to an object of any one of the data type.  
Pointer declaration:
Syntax : data type * variable_name;  for example : int *x;  or int* x; or int * x;

Pointer operator:
Pointer operator can be represented as * (asterik) with a variable name. For example if a variable of integer data type and also declared * with another variable, it means the variable is of type  “pointer to integer”.
int *ptr    ;
float  *fptr;
char  *cptr;

Address operator:
An address operator can be represented by the use of & (ampersand). The operator immediately preceding a variable returns the address the of the variable.
E,g   int  a;
        Int *p;
        p = &a ; or  int *p = &a; // address of variable a is assigned to pointer variable p.
& operator can be used only with a simple variable or an array element .
  E.g  &120  ;   //invalid
          int x[10];
          &x;            //illegal use of ampersand operator.
           &x[0]   or  &x[i]       //valid;

Constant pointer:  

C++ adds the concept of constant pointer  and a pointer to a constant.
           char * const ptr1=”GOOD”;      // constant pointer
   we can not modify the address that ptr1 is initialized to.
           int const * ptr2 = &m;    // pointer to a constant
   ptr2 is declared as pointer to constant> It can point to any variable of correct type but the contents of what  
   it points to cannot cannot be changed.
   We can also declare both the pointer and the variable as constants in the following way:
           Const char * const ptr = “xyz”;
   This statement declares ptr as a constant pointer  to the string which has been declared a constant. In this case neither the address assigned to the pointer ptr nor the contents it points to can be changed.

Void pointer:
    Syntax: void *variable        for example:  void *x;
The void type of pointer is a special type of pointer; void pointers are pointers that point to a value that has no type. This allows void pointers to point to any data type, from an integer value or a float to a string of characters.
           int .a;
           char c;
           void  *p;
           p = &a;
           cout<<” Value of a=” , *((int *)p);    //void pointer is typecasted to int type.
Null pointer:
A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.
    Syntax:     data type * variable = NULL     or         data type *variable = 0
    For example   int *x = 0     or int *x = NULL:

Pointer Arithmetic:
ptr--  :     Decrements the value by the size of the data type.
ptr++:     Increments the value by the size of the data type.
--ptr :     Decrements the value by the size of the data type before the statement is executed.  
++ptr :    Increments the value by the size of the data type before the statement is executed.
*ptr--:     Retrieve the contents of location pointed by pointer then decrement the pointer.
*--ptr:     Decrement the pointer then retrieve the contents of new location pointed by pointer.
*ptr++:   Retrieve the contents of location pointed by pointer then increment the pointer.
*++ptr:   Increment the pointer then retrieve the contents of new location pointed by pointer.
--(*ptr):   Decrement the contents pointed by pointer by one then retrieve the new value.
(*ptr)--:   First retrieve the content pointed by pointer , then decrement the content by one pointed by
               pointer.
++(*ptr):   Increment the contents pointed by pointer by one then retrieve the new value.
(*ptr)++:   First retrieve the content pointed by pointer , then increment the content by one pointed by
                pointer.

Labels: