• Now Online : 100
  • admin@codemyne.net

Pointers

A brief discussion on pointers

A pointer is a variable that holds the address of objects in memory. Through a pointer is an object can be referenced indirectly. The statement

        int *ptr;

Declares a variable ptr which is a pointer that may point to (i.e.. hold the address of ) any variable of type int. The unary operator ‘*’ makes all the difference between an ordinary variable and a pointer type variable. This operator also called as indirection or dereferencing operator, when used in a declaration, indicates that the variable is a pointer variable. It is also used to get the value of whatever ptr is pointing to. In a pointer declaration, the variable must always be preceded by an asterisk (‘*’) symbol.

Initializing of pointers:

It is always good practice to initialize pointers as soon as they are declared. Initializing a pointer involves the assigning of the address of a variable to the pointer. In order to make a pointer hold the address of a variable, say x, the statements

        int x ;
        ptr=&x;

Must be used. Here the operator ‘&’ or the address operator returns the address of the variable x and assigns to the ptr. A pointer can be initialized even when it is declared. For example, the declaration,

        int x;
        int *ptr =&x;         is perfectly valid.

Note:

as mentioned earlier, it is good programming practice to initialize variables as soon as they are declared. An uninitialized pointer may cause huge programs to crash because it is pointing some unnamed location in memory.

Using Pointers:

Pointers in C++, if used properly, give way to faster programs and efficient code. But if they are misused, they could lead to huge programs crashing, without as much as an iota of warning.

Pointer Manipulation:

Complicated manipulation of pointers is possible through the use of the address and the dereferencing operators and also by combining them with relational operators ’+’ and ‘-‘ now observe the following code segment

int x = 1,y= 2 ;
int *xptr,*yptr;
xptr = &x; // xptr and yptr are pointing 
yptr = &y;    // to x and y respectively 
y= *yptr + 1 ; // get what yptr if pointing to, add one to it and assign the result to y
yptr =xptr;  //yptr in also pointing to x now
yptr =&y  ; // the address of y is assign to yptr
*yptr = *xptr;  // yptr is made equal to whatever xptr is pointing to 

Note that in the above code segment, lines 6 and 8 are not equal. In line 6 yptr is made to point to whatever xptr is pointing to, y remains the same. Whereas in line 8, the value of what yptr is pointing to is made equal to that of what xptr is pointing to. The value of y is automatically changed. It is made equal to one.

Example 1:

#include<iostream.h>
  
int main()
{   
    int firstvalue = 5 ,secondvalue = 15;
    int *p1,*p2;
    p1 =&firstvalue; //p1=address of firstvalue
    p2  = &secondvale; //p2=address of secondvalue
    *p1 = 10; //value pointed by p1=10
    *p2 = *p1; //value pointed by p2=value pointed by p1
    p1 =p2; //p1=p2 (value of pointer is copied)
    *p1 = 20 ; //value pointed by p1=20
    cout<<"firstvalue is "<< firstvalue <<endl;
    cout<<"second value is "<<secondvale <<endl;
    return 0 ;
}

Example 2:

#include<iostream.h>
  
int main()
{   
    int numbers[5];
    int *p;
    p = numbers ;
    *p = 10;
    p++;
    *p = 20;
    p = &numbers [2];
    *p = 30;
    p =numbers + 3 ;
    *p = 40;
    p=numbers ;
    *(p+4) = 50;
    for (int n=0;n<5;n++)
    {
        cout<<numbers [n]<<",";
        return 0;
    }
}

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment