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


0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (745) Python Coding Challenge (198) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses