POINTERS IN
C++
POINTERS IN
C++
Present by -
Suraj R. Maurya
Topics :
All About Pointers :
Definition
Declaring Pointer Variable
&(‘Address of’ Operator)
*(‘Value of’ or Indirection Operator)
Pointers Type
Pointer Arithmetic
 Pass by Pointer in function
 Array of Pointer
String of Pointer
Object of Pointer
this Pointer
Advantages of Pointer
Limitations of Pointer
100… … 1024 …
Memory address: 1024 10321020
A
Variable a’s value, i.e., 100, is
stored at memory location 1024
Computer Memory
• Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
• Eg: int a=100;
• Every byte in computer memory has an
address.
• Addresses are numbers just like house
numbers.
• Addresses start from 0 and go up as 1, 2,
3….e.g. in 640 KB memory addresses will
be 0 to 655359 I.e., (640 X 1024)-1.
• The moment a variable is declared,
memory is allocated in the RAM for its
storage. int X=100;.
• A variable in the RAM can be accessed
only if you know its address.
Definition:
A pointer is a variable which store the
memory address of another variable.
It supports dynamic memory allocation
routines.
A pointer variable and normal variable is
always same data type.
Declaration And Initialization Of
Pointer Variable
• Syntax :
• Datatype *variable_name;
• eg. int *x; float *y; char *z;
• Address of operator(&)- it is
a unary operator that returns the
memory address of its operand.
Here the operand is a normal
variable.
• Ptr_name=&variable_name;
• eg. int x = 10;
int *ptr ;
ptr= &x;
• Now ptr will contain address
where the variable x is stored
in memory 10
0x5f0fff2
ptr
x
Address
of x
“*”(‘Value of’ or Indirection Operator)
 It is a unary operator that returns the value stored
at the address pointed to by the pointer.
 Here the operand is a pointer variable.
• eg.
• int x = 10;
• int *ptr = &x;
• cout<< ptr;// address stored at ptr will be displayed
• cout<<*ptr;// value pointed to by ptr will be
displayed
• Now ptr can also be used to change/display the value
of x.
Pointers Type
Pointer Arithmetic
• Two arithmetic operations, addition and
subtraction, may be performed on pointers.
When we add 1 to a pointer, we are actually
adding the size of data type in bytes, the pointer
is pointing at.
• For e.g. int *x; x++;
• If current address of x is 1000, then x++
statement will increase x by 2(size of int data
type) and makes it 1002, not 1001.
• Consider p1 and p2 as pointers:
P1=&a;p2=&b;
• The arithmethic operations that can be
performed on p1 and p2 are summarized in
the table below:
Pointer Expressions:
• A Pointer expression contains pointer variable
as an operand and arithmetic operator such
as +,-,*,/
• Eg: (*p1)++ : This statement increments value,
stored at the memory address pointed by
pointer p1 by 1.
Pass by Pointer in function
Consider the program of swapping two
variables with the help of pointers:
#include<iostream.h>
void swap(int *m, int *n)
{ int temp; temp = *m; *m = *n; *n = temp; }
void main()
{ int a = 5, b = 6;
cout << “n Value of a :” << a << “ and b :” <<
b;
swap(&a, &b);
cout << “n After swapping value of a :” << a
<< “and b :” << b;
}
COMPARING PASS BY VALUE, PASS BY
REFERENCE AND PASS BY POINTER
PASS BY VALUE PASS BY REFERENCE PASS BY POINTER
Separate memory is
allocated to formal
parameters.
Formal and actual
parameters share the
same memory space.
Formal parameters
contain the address of
actual parameters.
Changes done in
formal parameters
are not reflected in
actual parameters.
Changes done in formal
parameters are
reflected in actual
parameters.
Changes done in formal
parameters are reflected
in actual parameters.
For eg.
void cube(int x)
{ x= x*x*x; }
void main()
{int y=10;
cout<<y<<endl;
cube(y);
cout<<y<<endl;}
output:
1010
For eg.
void cube(int &x)
{ x= x*x*x; }
void main()
{int y=10;
cout<<y<<endl;
cube(y);
cout<<y<<endl;}
output:
101000
For eg.
void cube(int *x)
{ *x= (*x)*(*x)*(*x); }
void main()
{int y=10;
cout<<y<<endl;
cube(&y);
cout<<y<<endl;}
output:
101000
Array of Pointer
• Array is collection of similar data type.
• We can create pointer variable with array both
are similar data type.
• Using array of pointer searching, insertion,
deletion etc. operation we can perform.
• Syntax:
Data_type variable_name[size of array];
Data_type *pointer_variable;
pointer_variable=variable_name;
OR
pointer_variable=&variable_name[0];
• Example of pointer of array:
Void main()
{
int a[2],*ptr;
Ptr=&a[0];
Cout<<“Ptr :”<<ptr;
*Ptr++;
}
String of Pointer
• Array of character is called string.
• We can create pointer variable with string both
are similar data type.
• Using string of pointer searching, finding length ,
comparisons ,concatenation ,reverse etc
operation we can perform.
• Syntax:
Data_type variable_name[size of string];
Data_type *pointer_variable;
Pointer_variable=&variable_name[0];
• Example of string of pointer:
void main()
{
Char name[4]=“NEEL”,*ptr;
Ptr=&name[0];
While(*ptr!=‘0’)
{
Ptr++;
Cout<<*ptr;
}
}
Object of Pointer
• When address of an object of a class is stored
into the pointer variable of the same class
type then it is pointer of object.
• This pointer can be used to access the data
member and member function of same class.
Syntax:
• Class_name object_name;
• Class_name *pointer_variable;
• Pointer_variable=&object_name;
• Pointer_variable->function_name();
Example of pointer to object:
• //create a class syco;
• //and declare data member and function;
• //Now, in main function:
Syco obj;
Syco *ptr;
Ptr=&obj;
Obj->getdtat();
Obj->putdata();
this Pointer
• C++ uses a unique keyword called “this” to
represent an object that invokes a member
function.
• This is a pointer that points to the object for
which this function was called.
• This is a unique pointer is automatically
passed to a member function when it is called.
• The pointer this acts as an implicit argument
to all the member function.
• its an in-built pointer so programmer doesn’t
need to explicitly create this pointer.
• One class can have exactly one this pointer.
• Syntax:
There are two ways of accessing member
with this pointer as follows:
this->data_member;
this->member_function();
OR
*this(dtat_member);
Example of this pointer:
Class abc
{ Int a;
Public:
Void getdata()
{
}};
The private variable ‘a’ can be used directly inside a
member function, like a=123;
But in this concept(in main function) to do same
job:
This->a=123;
This ->getdata();
Advantages of Pointer
I. Pointer can handle array more efficiently.
II. As they provide direct access to memory, they
process data very fast.
III. They reduce storage space.
IV. They give alternate way to access elements of an
array.
V. Pointers avoid any confusion by the compiler due to
variables with same name.
VI. They help in building complex data structures like
linked list , stacks , queues , trees , graphs etc.
Limitations of Pointer
i. They are slower than normal variables.
ii. Pointers store only address but cannot store
a value.
iii. It requires a null pointer reference.
iv. If a pointer is initialized with incorrect value ,
it can lead to a garbage value which may
cause error in the program.

Pointer in C++

  • 1.
  • 2.
  • 3.
    Topics : All AboutPointers : Definition Declaring Pointer Variable &(‘Address of’ Operator) *(‘Value of’ or Indirection Operator) Pointers Type Pointer Arithmetic  Pass by Pointer in function
  • 4.
     Array ofPointer String of Pointer Object of Pointer this Pointer Advantages of Pointer Limitations of Pointer
  • 5.
    100… … 1024… Memory address: 1024 10321020 A Variable a’s value, i.e., 100, is stored at memory location 1024 Computer Memory • Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there • Eg: int a=100;
  • 6.
    • Every bytein computer memory has an address. • Addresses are numbers just like house numbers. • Addresses start from 0 and go up as 1, 2, 3….e.g. in 640 KB memory addresses will be 0 to 655359 I.e., (640 X 1024)-1. • The moment a variable is declared, memory is allocated in the RAM for its storage. int X=100;. • A variable in the RAM can be accessed only if you know its address.
  • 7.
    Definition: A pointer isa variable which store the memory address of another variable. It supports dynamic memory allocation routines. A pointer variable and normal variable is always same data type.
  • 8.
    Declaration And InitializationOf Pointer Variable • Syntax : • Datatype *variable_name; • eg. int *x; float *y; char *z; • Address of operator(&)- it is a unary operator that returns the memory address of its operand. Here the operand is a normal variable. • Ptr_name=&variable_name;
  • 9.
    • eg. intx = 10; int *ptr ; ptr= &x; • Now ptr will contain address where the variable x is stored in memory 10 0x5f0fff2 ptr x Address of x
  • 10.
    “*”(‘Value of’ orIndirection Operator)  It is a unary operator that returns the value stored at the address pointed to by the pointer.  Here the operand is a pointer variable. • eg. • int x = 10; • int *ptr = &x; • cout<< ptr;// address stored at ptr will be displayed • cout<<*ptr;// value pointed to by ptr will be displayed • Now ptr can also be used to change/display the value of x.
  • 11.
  • 13.
    Pointer Arithmetic • Twoarithmetic operations, addition and subtraction, may be performed on pointers. When we add 1 to a pointer, we are actually adding the size of data type in bytes, the pointer is pointing at. • For e.g. int *x; x++; • If current address of x is 1000, then x++ statement will increase x by 2(size of int data type) and makes it 1002, not 1001.
  • 14.
    • Consider p1and p2 as pointers: P1=&a;p2=&b; • The arithmethic operations that can be performed on p1 and p2 are summarized in the table below:
  • 15.
    Pointer Expressions: • APointer expression contains pointer variable as an operand and arithmetic operator such as +,-,*,/ • Eg: (*p1)++ : This statement increments value, stored at the memory address pointed by pointer p1 by 1.
  • 16.
    Pass by Pointerin function Consider the program of swapping two variables with the help of pointers: #include<iostream.h> void swap(int *m, int *n) { int temp; temp = *m; *m = *n; *n = temp; } void main() { int a = 5, b = 6; cout << “n Value of a :” << a << “ and b :” << b; swap(&a, &b); cout << “n After swapping value of a :” << a << “and b :” << b; }
  • 17.
    COMPARING PASS BYVALUE, PASS BY REFERENCE AND PASS BY POINTER PASS BY VALUE PASS BY REFERENCE PASS BY POINTER Separate memory is allocated to formal parameters. Formal and actual parameters share the same memory space. Formal parameters contain the address of actual parameters. Changes done in formal parameters are not reflected in actual parameters. Changes done in formal parameters are reflected in actual parameters. Changes done in formal parameters are reflected in actual parameters. For eg. void cube(int x) { x= x*x*x; } void main() {int y=10; cout<<y<<endl; cube(y); cout<<y<<endl;} output: 1010 For eg. void cube(int &x) { x= x*x*x; } void main() {int y=10; cout<<y<<endl; cube(y); cout<<y<<endl;} output: 101000 For eg. void cube(int *x) { *x= (*x)*(*x)*(*x); } void main() {int y=10; cout<<y<<endl; cube(&y); cout<<y<<endl;} output: 101000
  • 18.
    Array of Pointer •Array is collection of similar data type. • We can create pointer variable with array both are similar data type. • Using array of pointer searching, insertion, deletion etc. operation we can perform. • Syntax: Data_type variable_name[size of array]; Data_type *pointer_variable; pointer_variable=variable_name; OR pointer_variable=&variable_name[0];
  • 19.
    • Example ofpointer of array: Void main() { int a[2],*ptr; Ptr=&a[0]; Cout<<“Ptr :”<<ptr; *Ptr++; }
  • 20.
    String of Pointer •Array of character is called string. • We can create pointer variable with string both are similar data type. • Using string of pointer searching, finding length , comparisons ,concatenation ,reverse etc operation we can perform. • Syntax: Data_type variable_name[size of string]; Data_type *pointer_variable; Pointer_variable=&variable_name[0];
  • 21.
    • Example ofstring of pointer: void main() { Char name[4]=“NEEL”,*ptr; Ptr=&name[0]; While(*ptr!=‘0’) { Ptr++; Cout<<*ptr; } }
  • 22.
    Object of Pointer •When address of an object of a class is stored into the pointer variable of the same class type then it is pointer of object. • This pointer can be used to access the data member and member function of same class.
  • 23.
    Syntax: • Class_name object_name; •Class_name *pointer_variable; • Pointer_variable=&object_name; • Pointer_variable->function_name();
  • 24.
    Example of pointerto object: • //create a class syco; • //and declare data member and function; • //Now, in main function: Syco obj; Syco *ptr; Ptr=&obj; Obj->getdtat(); Obj->putdata();
  • 25.
    this Pointer • C++uses a unique keyword called “this” to represent an object that invokes a member function. • This is a pointer that points to the object for which this function was called. • This is a unique pointer is automatically passed to a member function when it is called. • The pointer this acts as an implicit argument to all the member function. • its an in-built pointer so programmer doesn’t need to explicitly create this pointer.
  • 26.
    • One classcan have exactly one this pointer. • Syntax: There are two ways of accessing member with this pointer as follows: this->data_member; this->member_function(); OR *this(dtat_member);
  • 27.
    Example of thispointer: Class abc { Int a; Public: Void getdata() { }}; The private variable ‘a’ can be used directly inside a member function, like a=123; But in this concept(in main function) to do same job: This->a=123; This ->getdata();
  • 28.
    Advantages of Pointer I.Pointer can handle array more efficiently. II. As they provide direct access to memory, they process data very fast. III. They reduce storage space. IV. They give alternate way to access elements of an array. V. Pointers avoid any confusion by the compiler due to variables with same name. VI. They help in building complex data structures like linked list , stacks , queues , trees , graphs etc.
  • 29.
    Limitations of Pointer i.They are slower than normal variables. ii. Pointers store only address but cannot store a value. iii. It requires a null pointer reference. iv. If a pointer is initialized with incorrect value , it can lead to a garbage value which may cause error in the program.