SlideShare a Scribd company logo
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++
Ad

More Related Content

What's hot (20)

Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
Neel Mungra
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Java program structure
Java program structureJava program structure
Java program structure
shalinikarunakaran1
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 

Viewers also liked (11)

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

Similar to Pointers in c++ (20)

C pointers and references
C pointers and referencesC pointers and references
C pointers and references
Thesis Scientist Private Limited
 
Pointers in c
Pointers in cPointers in c
Pointers in c
CHANDAN KUMAR
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
Information Technology Center
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Vijayananda Ratnam Ch
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Ameer Hamxa
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
Dushmanta Nath
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
TamiratDejene1
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Monishkanungo
 
Pointers
PointersPointers
Pointers
Swarup Boro
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
Ad

Recently uploaded (20)

wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint PresentationFrom Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
From Vibe Coding to Vibe Testing - Complete PowerPoint Presentation
Shay Ginsbourg
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 

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; }
  翻译: