Tuesday, 3 April 2018

Pointers

A pointer is a variable which holds the memory address of another variable.

Advantage of pointer :
 1. It supports dynamic allocation and de allocation of memory segments.
 2. It allows creation of data structure such as linked list, stacks, queues, trees and graphs.
 3. With the help of pointer , variable can be swapped without physically moving them.
 4. It allows to pass variable , arrays , function , strings an structure as a function argument.
 5. Pointer allows to return structure variable from functions.

A pointer variable of two parts:
 1. The Pointer operator
 2. The Address operator

Pointer Operator :
 1. A pointer variable can be represented by a combination of *(Asterisk) with a variable.
 2. If a variable of integer data type is declared with *(Asterisk) then if means the variable is of type 'pointer to integer'.
 3. int *ptr
     When ptr is a pointer variable which holds the address of an integer data type variable.
 4. All pointer variable must be declared before it is used in a program like any other variable.
 5. When a pointer variable is declared , the variable name must be preceded by an asterisk(*).
 6 The general format of declaration is
     Datatype *pointervariable;
     Ex : char *cptr;
            float *fptr;
            double *dptr;

The Address Operator :
 1. An address operator can be represented by the combination by the combination of '&'(ampersand) with the variable.
 2. The '&' is a unary operator the returns the memory address of its operand.
 3. ptr = &x;
      The meaning of this statement is 'ptr' is a pointer variable which receives the address of a variable 'x'.


WAP to display the content of a pointer

#include<conio.h>
#include<iostream.h>
void main( )
{
 int x;
int *ptr;
clrscr( );
cout<<"\n\tEnter Value of x";
cin>>x;
ptr=&x;
cout<<"\n\ntx--->"<<x"\t"<<"ptr is--->"<<ptr;
cout<<"\n\n\tx--->"<<x"\t"<<"*ptr is--->"<<*ptr;
getch( );
}

Output :-
  Enter Value of x15
 x--->15  ptr is--->0⤬8fc1fff4
 x--->  *ptr is--->15

WAP to assign the pointer variable to another pointer and display the content of both the pointer variable.

#include<conio.h>
#include<iostream.h>
void main( )
{
int x=12;
int *ptr1, *ptr2;
clrscr( );
ptr1=&x;
ptr2=ptr1;
cout<<"\n\n\tValue of x is--->"<<x;
cout<<"\n\n\tContent at ptr1 is--->"<<*ptr1;
cout<<"\n\n\tContent at ptr2 is--->"<<*ptr2;
getch( );
}

Output :-
 Value of x is--->12
 Content at ptr1 is--->12
 Content at ptr2 is--->12

WAP to display the content of the pointer variable using arithmetic operation.

#include<conio.h>
#include<iostream.h>
void main( )
{
int x,y;
int *ptr;
x=12;
ptr=&&x;
cout<<"\n\n\tValue of x is--->"<<x<<"\t"<<"Value at pointer is--->"<<*ptr;
y=*ptr+1;
cout<<"\n\n\tValue of y is--->"<<y<<"\t"<<"Value at pointer is--->"<<*ptr;
getch( );
}

Output :-
 Value of x is--->12                  Value at pointer is--->12
 Value of y is--->13                  Value at pointer is--->12

WAp to display the memory address of a variable using pointer before incrementation and decrementation and after incrementation and decrementation.

#include<conio.h>
#include<iostream.h>
void main( )
{
int x=12;
int *ptr1, *ptr2;
clrscr( );
ptr1=&x;
ptr2=&x;
cout<<"\n\n\tMemory Address before incrementation--->"<<ptr1<<endl;
cout<<"\n\n\tMemory Address before decrementation--->"<<ptr1<<endl;
ptr1++;
ptr2--;
cout<<"\n\n\tMemory Address after  incrementation--->"<<ptr1<<endl;
cout<<"\n\n\tMemory Address after decrementation--->"<<ptr1<<endl;
getch( );
}

Output :-
   Memory Address before incrementation--->0⤫8fd2fff4
   Memory Address before decrementation--->0⤫8fd2fff4
   Memory Address after incrementation--->0⤫8fd2fff6
   Memory Address before incrementation--->0⤫8fd2fff2


1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)