SlideShare a Scribd company logo
3
Most read
5
Most read
12
Most read
POINTERS INC++
BY -
SAI TARLEKAR
Points
 Introduction.
 Declaration and initialization of pointers.
 Small program.
 Thumb rule.
 Pointer Variable.
 Pointer Arithmetic.
 Reference Variable.
 Reference as function arguments
What is a Pointer???
 A pointer is a variable which holds the memory address
of another variable.
 This memory address is the location of another variable
where it has been stored as memory.
 Therefore if a variable contains the address of another
variable is said to point to the second.
Declaration and Initialization of
Pointers
 Syntax : Datatype *variable_name;
eg. int *x;
float *y;
char *z;
eg. int x = 10;
int *ptr = &x;
 Now ptr will contain address where the variable x is stored in memory.
int *ptr;
This tells the compiler three things about ptr:-
1) The (*) tells that the variable ptr is a pointer
variable.
2) Pointers need a memory location.
3) Pointers points to a variable of type int.
Program
Output:-
Pointers in c++
More info….
1) A pointer that is not initialzed is called as wild pointer
because you have no idea what it is pointing to.
2) A pointer whose value is zero is called as null pointer.
3) Void pointer - void pointer is a special type of pointer
that can be pointed at objects of any data type. A void
pointer is declared using data type void.
Thumb rule of pointers:-
1) Type must be same to the variable whose address is going
to pass to the pointer.
2) When you want to pass the address of the variable use ‘&’
before the variable name.
3) When you want to pass the value of the variable use ‘*’
before the pointer.
Pointers in c++
Pointer operator
 A pointer operator or a pointer variable is represented by a
combination of asterisk(*) with a variable name.
Syntax:-
data_type *pointer_variable;
eg. If a variable of character data type and also declared as *
(asterisk) with another variable it means the variable is of type
“Pointer to character”.
char *ptr;
where ptr is a pointer variable which holds the address of an
character data type.
Address Operator
 An address operator can be represented by a combination of and (ampersand)
with a pointer variable.
 The and is a unary operator that returns the memory address of its operand.
Syntax:-
data_type variable1;
data_type variable=&variable1;
Eg:-
int x;
int *ptr=&x;
It means that ptr receives the address of x;
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.
Eg. 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.
Data is stored in contiguous memory cell.
The Number of memory cells required to store a
data item depends on the type of data item
Char 1 byte
Integer 2 byte
Floating 4 byte
Double 8 byte
Program
Output:-
REFERENCE VARIABLE
A reference variable is a name that acts as an alias or an alternative name,
for an already existing variable.
Syntax:-
Data type &variable name = already existing variable;
Eg. int num=10;
int & sum = num; // sum is a reference variable or alias name for
num
NOTE: Both num and sum refer to the same memory location. Any changes made to the sum
will also be reflected in num.
10
sum
num
void cube(int &x)
{
x= x*x*x;
}
void main()
{
int y=10;
cout<<y<<endl;
cube(y);
cout<<y<<endl;
}
x
In the above program reference of y is passed. No separate
memory is allocated to x and it will share the same memory
as y. Thus changes made to x will also be reflected in y.
OUTPUT:
10
1000
REFERENCE AS FUNCTION ARGUMENTS
y
100
Program
#include<iostream.h>
#include<conio.h>
swap(int *,int *)
void main()
{
int a,b;
cout<<"nEnter value for A : ";
cin>>a;
cout<<"nEnter value for B : ";
cin>>b;
cout<<"nnBefore Swapping : ";
cout<<"nttta = "<<a;
cout<<"ntttb = "<<b;
swap(&a,&b);
cout<<"nnAfter Swapping : ";
cout<<"nttta = "<<a;
cout<<"ntttb = "<<b;
getch();
}
swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output:-
Pointers in c++

More Related Content

What's hot (20)

PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Constructor ppt
Vinod Kumar
 
PPTX
class and objects
Payel Guria
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Array of objects.pptx
RAGAVIC2
 
PPTX
Union in C programming
Kamal Acharya
 
PPTX
C functions
University of Potsdam
 
PPTX
Call by value
Dharani G
 
PPTX
Pointers in c++
Rajat Busheheri
 
PPTX
Pointers in c - Mohammad Salman
MohammadSalman129
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Recursive Function
Harsh Pathak
 
PPTX
Member Function in C++
NikitaKaur10
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPT
RECURSION IN C
v_jk
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Operator Overloading
Nilesh Dalvi
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
classes and objects in C++
HalaiHansaika
 
Constructor ppt
Vinod Kumar
 
class and objects
Payel Guria
 
Function in C program
Nurul Zakiah Zamri Tan
 
Array of objects.pptx
RAGAVIC2
 
Union in C programming
Kamal Acharya
 
Call by value
Dharani G
 
Pointers in c++
Rajat Busheheri
 
Pointers in c - Mohammad Salman
MohammadSalman129
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Recursive Function
Harsh Pathak
 
Member Function in C++
NikitaKaur10
 
Programming in c Arrays
janani thirupathi
 
Inheritance in c++
Vineeta Garg
 
Constructor and destructor
Shubham Vishwambhar
 
RECURSION IN C
v_jk
 

Viewers also liked (11)

PPT
Unit 6 pointers
George Erfesoglou
 
PPTX
Chp3(pointers ref)
Mohd Effandi
 
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
PPT
Pointers (Pp Tminimizer)
tech4us
 
PPT
Introduction to pointers and memory management in C
Uri Dekel
 
PDF
Pointers
sarith divakar
 
PDF
Pointer in c++ part1
Subhasis Nayak
 
PPTX
C++ Pointers
Chaand Sheikh
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
PPTX
pointers,virtual functions and polymorphism
rattaj
 
Unit 6 pointers
George Erfesoglou
 
Chp3(pointers ref)
Mohd Effandi
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
Pointers (Pp Tminimizer)
tech4us
 
Introduction to pointers and memory management in C
Uri Dekel
 
Pointers
sarith divakar
 
Pointer in c++ part1
Subhasis Nayak
 
C++ Pointers
Chaand Sheikh
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
pointers,virtual functions and polymorphism
rattaj
 
Ad

Similar to Pointers in c++ (20)

PDF
C pointers and references
Thesis Scientist Private Limited
 
PPTX
Pointers in c
CHANDAN KUMAR
 
PPTX
Pointers
Vardhil Patel
 
PDF
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
PPTX
pointers.pptx
janithlakshan1
 
PPTX
Pointers in c language
Tanmay Modi
 
PPTX
Pointers in c v5 12102017 1
tanmaymodi4
 
PDF
Pointers
Prasadu Peddi
 
PPT
Lect 9(pointers) Zaheer Abbas
Information Technology Center
 
PPTX
Pointers in C
Vijayananda Ratnam Ch
 
PPT
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
PPTX
pointers.pptx
s170883BesiVyshnavi
 
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
PPS
C programming session 05
Dushmanta Nath
 
PPTX
Pointer in C
bipchulabmki
 
PDF
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PDF
Pointers in C
Monishkanungo
 
PDF
Pointers
Swarup Boro
 
C pointers and references
Thesis Scientist Private Limited
 
Pointers in c
CHANDAN KUMAR
 
Pointers
Vardhil Patel
 
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
pointers.pptx
janithlakshan1
 
Pointers in c language
Tanmay Modi
 
Pointers in c v5 12102017 1
tanmaymodi4
 
Pointers
Prasadu Peddi
 
Lect 9(pointers) Zaheer Abbas
Information Technology Center
 
Pointers in C
Vijayananda Ratnam Ch
 
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
pointers.pptx
s170883BesiVyshnavi
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
C programming session 05
Dushmanta Nath
 
Pointer in C
bipchulabmki
 
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in C
Monishkanungo
 
Pointers
Swarup Boro
 
Ad

Recently uploaded (20)

PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PPT
Brief History of Python by Learning Python in three hours
adanechb21
 
PDF
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
PDF
Salesforce Pricing Update 2025: Impact, Strategy & Smart Cost Optimization wi...
GetOnCRM Solutions
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
What companies do with Pharo (ESUG 2025)
ESUG
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Brief History of Python by Learning Python in three hours
adanechb21
 
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
Salesforce Pricing Update 2025: Impact, Strategy & Smart Cost Optimization wi...
GetOnCRM Solutions
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Presentation about variables and constant.pptx
kr2589474
 
What companies do with Pharo (ESUG 2025)
ESUG
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 

Pointers in c++

  • 2. Points  Introduction.  Declaration and initialization of pointers.  Small program.  Thumb rule.  Pointer Variable.  Pointer Arithmetic.  Reference Variable.  Reference as function arguments
  • 3. What is a Pointer???  A pointer is a variable which holds the memory address of another variable.  This memory address is the location of another variable where it has been stored as memory.  Therefore if a variable contains the address of another variable is said to point to the second.
  • 4. Declaration and Initialization of Pointers  Syntax : Datatype *variable_name; eg. int *x; float *y; char *z; eg. int x = 10; int *ptr = &x;  Now ptr will contain address where the variable x is stored in memory.
  • 5. int *ptr; This tells the compiler three things about ptr:- 1) The (*) tells that the variable ptr is a pointer variable. 2) Pointers need a memory location. 3) Pointers points to a variable of type int.
  • 9. More info…. 1) A pointer that is not initialzed is called as wild pointer because you have no idea what it is pointing to. 2) A pointer whose value is zero is called as null pointer. 3) Void pointer - void pointer is a special type of pointer that can be pointed at objects of any data type. A void pointer is declared using data type void.
  • 10. Thumb rule of pointers:- 1) Type must be same to the variable whose address is going to pass to the pointer. 2) When you want to pass the address of the variable use ‘&’ before the variable name. 3) When you want to pass the value of the variable use ‘*’ before the pointer.
  • 12. Pointer operator  A pointer operator or a pointer variable is represented by a combination of asterisk(*) with a variable name. Syntax:- data_type *pointer_variable; eg. If a variable of character data type and also declared as * (asterisk) with another variable it means the variable is of type “Pointer to character”. char *ptr; where ptr is a pointer variable which holds the address of an character data type.
  • 13. Address Operator  An address operator can be represented by a combination of and (ampersand) with a pointer variable.  The and is a unary operator that returns the memory address of its operand. Syntax:- data_type variable1; data_type variable=&variable1; Eg:- int x; int *ptr=&x; It means that ptr receives the address of x;
  • 14. 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. Eg. 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.
  • 15. Data is stored in contiguous memory cell. The Number of memory cells required to store a data item depends on the type of data item Char 1 byte Integer 2 byte Floating 4 byte Double 8 byte
  • 18. REFERENCE VARIABLE A reference variable is a name that acts as an alias or an alternative name, for an already existing variable. Syntax:- Data type &variable name = already existing variable; Eg. int num=10; int & sum = num; // sum is a reference variable or alias name for num NOTE: Both num and sum refer to the same memory location. Any changes made to the sum will also be reflected in num. 10 sum num
  • 19. void cube(int &x) { x= x*x*x; } void main() { int y=10; cout<<y<<endl; cube(y); cout<<y<<endl; } x In the above program reference of y is passed. No separate memory is allocated to x and it will share the same memory as y. Thus changes made to x will also be reflected in y. OUTPUT: 10 1000 REFERENCE AS FUNCTION ARGUMENTS y 100
  • 20. Program #include<iostream.h> #include<conio.h> swap(int *,int *) void main() { int a,b; cout<<"nEnter value for A : "; cin>>a; cout<<"nEnter value for B : "; cin>>b; cout<<"nnBefore Swapping : "; cout<<"nttta = "<<a; cout<<"ntttb = "<<b; swap(&a,&b); cout<<"nnAfter Swapping : "; cout<<"nttta = "<<a; cout<<"ntttb = "<<b; getch(); } swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; }