SlideShare a Scribd company logo
3
Most read
4
Most read
5
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

PPTX
Pointer in C++
PPTX
Pointers in c++
PPT
C++ classes tutorials
PPTX
Dynamic memory allocation in c++
PPTX
Constructors and destructors
PDF
Pointers & References in C++
PPT
C++ Arrays
PPTX
Arrays In C++
Pointer in C++
Pointers in c++
C++ classes tutorials
Dynamic memory allocation in c++
Constructors and destructors
Pointers & References in C++
C++ Arrays
Arrays In C++

What's hot (20)

PPTX
Type conversion
PPTX
Functions in c++
PPT
RECURSION IN C
PPTX
Pointers in c++
PPTX
07. Virtual Functions
PPTX
Static Data Members and Member Functions
PDF
Function overloading ppt
PPTX
Scope rules : local and global variables
PPTX
Pointers in C Programming
PPTX
Union in C programming
PPTX
Pointers in C
PDF
Arrays In C
PPT
Pointers in c
PDF
Pointers in C
PPTX
Exception handling c++
PPTX
Storage class in C Language
PPTX
Dynamic memory allocation
PPT
Pointers C programming
PPTX
Presentation on pointer.
PPT
Class and object in C++
Type conversion
Functions in c++
RECURSION IN C
Pointers in c++
07. Virtual Functions
Static Data Members and Member Functions
Function overloading ppt
Scope rules : local and global variables
Pointers in C Programming
Union in C programming
Pointers in C
Arrays In C
Pointers in c
Pointers in C
Exception handling c++
Storage class in C Language
Dynamic memory allocation
Pointers C programming
Presentation on pointer.
Class and object in C++
Ad

Viewers also liked (11)

PPT
Unit 6 pointers
PPTX
Chp3(pointers ref)
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
PPT
Pointers (Pp Tminimizer)
PPT
Introduction to pointers and memory management in C
PDF
Pointers
PDF
Pointer in c++ part1
PPTX
C++ Pointers
PPSX
INTRODUCTION TO C PROGRAMMING
PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
PPTX
pointers,virtual functions and polymorphism
Unit 6 pointers
Chp3(pointers ref)
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Pointers (Pp Tminimizer)
Introduction to pointers and memory management in C
Pointers
Pointer in c++ part1
C++ Pointers
INTRODUCTION TO C PROGRAMMING
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
pointers,virtual functions and polymorphism
Ad

Similar to Pointers in c++ (20)

PPTX
Used of Pointer in C++ Programming
PPTX
4 Pointers.pptx
PPTX
Algoritmos e Estruturas de Dados - Pointers
PPT
Advanced pointers
PPT
pointers CP Lecture.ppt
PPTX
PPTX
Pointers in c v5 12102017 1
PPTX
Pointers in c language
PPTX
POINTERS IN C
PDF
VIT351 Software Development VI Unit3
PDF
C++ Pointers , Basic to advanced Concept
ODP
Pointers in c++ by minal
PDF
Pointers in Programming
PDF
Pointers in c++
PDF
Chapter 5 (Part I) - Pointers.pdf
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPT
detailed information about Pointers in c language
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
Used of Pointer in C++ Programming
4 Pointers.pptx
Algoritmos e Estruturas de Dados - Pointers
Advanced pointers
pointers CP Lecture.ppt
Pointers in c v5 12102017 1
Pointers in c language
POINTERS IN C
VIT351 Software Development VI Unit3
C++ Pointers , Basic to advanced Concept
Pointers in c++ by minal
Pointers in Programming
Pointers in c++
Chapter 5 (Part I) - Pointers.pdf
c++ pointers by Amir Hamza Khan (SZABISTIAN)
detailed information about Pointers in c language
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
358 33 powerpoint-slides_3-pointers_chapter-3

Recently uploaded (20)

PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Patient Appointment Booking in Odoo with online payment
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
Nekopoi APK 2025 free lastest update
PDF
Cost to Outsource Software Development in 2025
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
AutoCAD Professional Crack 2025 With License Key
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
CHAPTER 2 - PM Management and IT Context
L1 - Introduction to python Backend.pptx
Patient Appointment Booking in Odoo with online payment
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Advanced SystemCare Ultimate Crack + Portable (2025)
Nekopoi APK 2025 free lastest update
Cost to Outsource Software Development in 2025
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Designing Intelligence for the Shop Floor.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Odoo Companies in India – Driving Business Transformation.pdf
AutoCAD Professional Crack 2025 With License Key
wealthsignaloriginal-com-DS-text-... (1).pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Autodesk AutoCAD Crack Free Download 2025
Oracle Fusion HCM Cloud Demo for Beginners
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)

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; }