Pointer in C++


Pointer in C++

Pointer in C++


What is Pointer ?

A pointer is a variable which contains the address of some other (another) variable.

It is a way through which location of the variable can be directly manipulated when required.



Declaration and initialization of Pointer

Pointer variable declared as a normal variable in addition with *character.
type *var_name;

Ex. int *ptr;

ptr stores the address of integer variable.

           int i=25;
           int *ptr;                                             ( *  is dereference operator)
           ptr=&i;                                              (& is reference operator)
  
Pointer in C++


*ptr will return the value stored in location 1003.

            &i returns 1003
             *ptr returns 25   



Dynamic memory allocation/ deallocation

Dynamic memory is the ability of the C++ program to acquire block of memory from dynamic memory pool called as free store or heap, during run time of the program. There are two type of memory management operators in C++.

o   New
o   Delete


°     New

It is used to dynamically allocate memory.

Syntax

pointer_variable=new data-type
int *a=new int;
*a=40;

Or

int *a=new int (40);



°  Delete

To ensure safe and efficient use of memory it is required that the memory reserved by the new operator must be released whenever it is now required in the program.

Syntax

delete pointer _variable

Ex. delete ptr;



Program to understand the concept of pointer

#include<iostream.h>
void main()
{
  int i=15;
  int *ptr;
  ptr=&i;
  cout<<”\n Value of i is:”<<i;
  cout<<”\n Address of i is”<<&i;
  cout<<”\n Value of i is:”<<*ptr;
  cout<<”\n Address of i is:”<<ptr;
}


Pointer and Array

There is strong relationship between pointer and array. The pointer version is faster than the array version of program.

Ex. int list[10];

Pointer in C++


cout<<*list;
It will display 6 as list[0]
*(list+1) =7



Program to calculate the sum of all array elements using array variable and pointer variable.

#include<iostream.h>
void main()
{
  int a[10];
  cout<<”Enter the elements of array”;
  int sum=0;
  for (i=0; i<10; i++)
  {
    cout<<”Enter the elements of array”;
    cin>>a[i];
 sum=sum+ a[i];
 }
 cout<<”The sum is:”<<sum;
}

Using pointer variable-
for (int *p=a; p<a+10;p++)
{
 cout<<”Enter elements”;
 cin>>a[i];
 sum=sum+ *p;
}
cout<<”The sum is:”<<sum;
}


Array of pointers

Pointer also arrayed that like any other data type.

 int *ip[10];                           //array of 10 integer pointer



Program to print different values being pointing to by an array of pointers.

#include<iostream.h>
void main()
{
 int *ip=4;
int f=65,s=67, t=69, fo=70, fi=75;
ip[0]=&f, ip[1]=&s; ip[2]=&t; ip[3]=&fo; ip[4]=&fi;
for (int i=0;i<4;i++)
{
 cout<<”The pointer ip[“ <<”] points to”<<*ip[i]<<”\n”;
 cout<<”The base address of array ip of pointer is”<<ip<<”\n”;
}


Difference between call by reference and call by pointer method

Both permit the variable to be modified in the calling program.

In call by reference method a reference or alias of the original variable is created, while in call by pointer method pointer to the original variable is created.



Pointer to structure

#include<iostream.h>
#include<string.h>
struct emp
{
  char ename[20];
  int ecode;
  float salary;
};
void main()
{
  emp e,*sptr;
  sptr=&e;
  strcpy(sptr     ename,”Mr.Adish” );
  sptr  -> ecode=1001;
  sptr -> salary=31000.56;
  cout<<sptr-> ename<<endl<<sptr->ecode<<endl<<sptr-> salary<<endl;
}



 Self referencing structure

As the name suggested a structure which contains a reference to itself. When a member of a structure is declared as a pointer to the structure itself then the structure is called as self referential structure.

Ex. linked list, stack, queue, trees.

struct node
{
  int data;
  node *next;
};


Pointer in C++

Data member ‘next’ of the structure can point to structure variable of type node.


Reference variables

A reference is nothing more than an alias. Anything you do with a reference you are actually doing to the object you are referencing. It is declared by using an ampersand (&).

Ex.  An alias y for the variable x can be created as

     int x;
     int &y=x;
     x=15;

Note- the reference in this concept does not mean “address of” it means “reference to”.


Pointer in C++




This post is about Pointer in  C++. 




Post a Comment

0 Comments