C++ language

adplus-dvertising
Pointers IN C++
Previous Home Next

Pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.

Pointer declaration:

A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable.

type * variable name

//Example:
int *ptr ;
float *string ;

Hera * is the dereference operator

Program to illustrate pointer declaration:

# include <iostream.h>
# include <conio.h>
void main ()
{
int *ptr ;
int sum ;
sum = 39 ;
ptr = ?
Cout << sum ;
Cout << ptr ;
}

Address operator:

Once we declare a pointer variable we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following eg

ptr = &num ;

& is the address of operator.

This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.

Example:


# include <iostream.h>
# include <conio.h>
int main()
{
 int i = 9 ;
Cout << i ;
Cout << &i ;    /*   Print the memory address  */
Cout << *(&i) ;  /*   It will give the value   */
return (0) ;
}

Program to display the content of variable their address using pointer variable:

# include <iostream.h>
# include <conio.h>
void main ()
{
int num, *intptr ;
float x , *floptr ;
char ch , *cptr ;
num = 123 ;
x = 12.34 ;
ch = ’a' ;
intptr = &x ;
cptr = &ch ;
floptr = &x ;
Cout << “Num stored at address:” << *intptr << intptr ;
Cout << “Value stored at address:” << *floptr << floptr ;
Cout << “Character stored at address:” << *cptr << cptr ;
}

Difference between pointers and reference variable:

  1. Pointer in its life cycle can refer any number of variables. But a reference variable can refer any one variable.
  2. A single pointer can contain different different addresses. But a single reference variable contain address of single variable.

Pointer airthmetic:

Only addition and subtraction can be done in pointers and no other operations can be performed.

Program of Pointer expression and Pointer airthmetic:

# include <iostream.h>
# include <conio.h>
void main ()
{
int ptr1 , ptr2;
int a ,b ,x ,y ,z ;
a = 40 ; b = 6;
ptr1 = &a ;
ptr2 = &b ;
x = *ptr1 + *ptr2 – 6 ;
y = 6* - *ptr1 / *ptr2 + 40 ;
Cout << “Address of a:” << ptr1 ;
Cout << “Address of b:” << ptr2 ;
Cout << a << b ;
Cout << x << y ;
ptr1= ptr1 + 60 ;
ptr2 = ptr2 ;
Cout << a << b ;
}

Pointers to Function:

The pointers are very much used in function declaration. Sometimes only with a pointer a complex function can be easily represented and success. The usage of pointer in a function definition may be classified into two groups:

  1. Call by value
  2. Call by reference
  1. Call by value: We have seen that a function is invoked there will be a link established between the formal and actual parameters. A temporary storage is created where the value of actual parameters is stored. The formal parameters picks up its value from storage area the mechanism of data transfer between actual and formal parameters allows the actual parameters mechanism of data transfer is referred as call by value. The corresponding formal parameter represents a local variable in the called function. The current value of corresponding actual parameter becomes the initial value of formal parameter. The value of formal parameter may be changed in the body of the actual parameter. The value of formal parameter may be changed in the body of the subprogram by assignment or input statements. This will not change the value of actual parameters.
  2. Example:

    # include <iostream.h>
    # include <conio.h>
    void swap (int x , int y)
    {
      int t ;
      t = x ;
      x = y ;
      y = t ;
    }
    int main ()
    {
      int a = 20 , b = 22 ;
      swap (a , b) ;
      Cout << a << b ;
    }
    
    Output a=20 ,b=22
  3. Call by reference: When we pass address to a function the parameters receiving the address should be pointers. The process of calling a function by using pointers to pass the address of the variable is known as call by reference. The function which is called by reference can change the values of the variable used in the call.
  4. Example:

    # include <iostream.h>
    # include <conio.h>
    void swap (int &x , int &y)
    {
      int t ;
      t = x ;
      x = y ;
      y = t ;
    }
    int main ()
    {
      int a = 20 , b = 22 ;
      swap (a , b) ;
      Cout << a << b ;
    }
    
    Output a=20 ,b=22

Pointers to Array:

An array is actually very much like pointer. We can declare the arrays first element as a[0] or as int *a because a[0] is an address and *a is also an address the form of declaration is equivalent. The difference is pointer is a variable and can appear on the left of the assignment operator that is lvalue. The array name is constant and cannot appear as the left side of assignment operator.

Program to display content of array using pointer:

# include <iostream.h>
# include <conio.h>
void main ()
{
int a [60] ;
int i , j , n ;
Cout << "Enter the elements of the array" ;
Cin >> n ;
Cout << "Enter the array elements" ;
for (I = 0 ; I < n ; I++)
Cin >> a[I] ;
Cout << "Array element are" ;
for( ptr = a , ptr < (a+n) ; ptr++)
Cout << *ptr , ptr ;
}

Strings are characters arrays and here last element is \0 arrays and pointers to char arrays can be used to perform a number of string functions.

Previous Home Next