SlideShare a Scribd company logo
Name – Amir hamza khan 
Instructor –Sir Irfan Latif Memon 
Section – BSCS-1 
RegNo. – 1312118
 Introduction to pointers. 
 The addresss of operator “&”. 
 Pointers and the “*” operator. 
 Pointers and function parameters. 
 Size of operators. 
Relation between array and pointers.
 The pointer is a variable that holds a memory address. 
 To understand the pointers we should first understand a bit 
about the computer memory. 
 Whenever any application is executed, it is loaded in to the 
memory form the disk and all the elements and components of 
that application will be located at the particular location 
somewhere in the memory.
 Computer memory is divided into sequentially numbered memory 
locations and each variable is located at a unique location in 
memory, known as its address. 
 The address of these memory locations is typically represented in 
Hexadecimal format with the prefix 0x before the number (e.g. 
0x8f4ffff2).
 As you know every variable is a memory location and every 
memory location has its address defined which can be accessed 
using ampersand (&) operator which denotes an address in 
memory 
 To return the address of the certain variables the address-of 
operator is placed in front of the variable name as, 
 cout<<&var1; 
 cout<<&var2; 
 cout<<&var3; 
 Now the “&” used with the variables in the cout statement will 
return the addresses at which these variable are located into the 
memory.
 #include <iostream.h> 
 main () 
 { 
 int var1; 
 char var2[10]; 
 cout << "Address of var1 
variable: "; 
 cout << &var1 << endl; 
 cout << "Address of var2 
variable: "; 
 cout << &var2 << endl; 
 }
 A pointer is a variable whose value is the address of another 
variable. Like any variable or constant, you must declare a 
pointer before you can work with it. The general form of a 
pointer variable declaration is: 
 Type *var-name; 
 Here, type is the pointer's base type; it must be a valid C++ type 
and var-name is the name of the pointer variable. The asterisk 
you used to declare a pointer is the same asterisk that you use 
for multiplication. However, in this statement the asterisk is 
being used to designate a variable as a pointer.
 int *ip; // pointer to an integer 
 double *dp; // pointer to a double 
 float *fp; // pointer to a float 
 char *ch; // pointer to character 
 The actual data type of the value of all pointers, whether 
integer, float, character, or otherwise, is the same, a long 
hexadecimal number that represents a memory address. The only 
difference between pointers of different data types is the data 
type of the variable or constant that the pointer points to
 It is always a good practice to assign the pointer NULL to a 
pointer variable in case you do not have exact address to be 
assigned. This is done at the time of variable declaration. A 
pointer that is assigned NULL is called a null pointer
 #include <iostream.h> 
 main () 
 { 
 int *ptr = NULL; 
 Cout<<“the value of ptr 
is” <<ptr<<endl; 
 
 }
 Example: Function to swap 
the values of two variables 
 #include <iostream.h> 
 void swap2(int* a, int* b) 
 { 
 int tmp; 
 tmp = *a; 
 *a = *b; 
 *b = tmp; 
 return; 
 } 
 int main() 
 { 
 int x = 1, y = 2; 
 swap2(&x, &y); 
 cout<<x<<y; 
 return 0; 
 }
 Pointers can be used to pass addresses of variables to called 
functions, thus allowing the called function to alter the values 
stored there. 
 We looked earlier at a swap function that did not change the 
values stored in the main program because only the values were 
passed to the function swap. 
 This is known as "call by value".
 If instead of passing the values of the variables to the called 
function, we pass their addresses, so that the called function 
can change the values stored in the calling routine. This is 
known as "call by reference" since we are referencing the 
variables. 
 The following shows the swap function modified from a "call by 
value" to a "call by reference". Note that the values are now 
actually swapped when the control is returned to main 
function.
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type. 
 The syntax of using sizeof is : 
 sizeof(data type) 
 The sizeof is a keyword, but it is a compile-time operator that 
determines the size, in bytes, of a variable or data type. 
 The sizeof operator can be used to get the size of classes, 
structures, unions and any other user defined data type.
#include <iostream.h> 
main() 
{ 
cout << "Size of char : " << sizeof(char) << endl; 
cout << "Size of int : " << sizeof(int) << endl; 
cout << "Size of short int : " << sizeof(short int) << endl; 
cout << "Size of long int : " << sizeof(long int) << endl; 
cout << "Size of float : " << sizeof(float) << endl; 
cout << "Size of double : " << sizeof(double) << endl; 
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; 
}
 Arrays and pointers are closely related 
◦ Array name is like constant pointer 
◦ Pointers can do array subscripting operations
 Accessing array elements with pointers 
◦ Assume declarations: 
int b[ 5 ]; 
int *bPtr; 
bPtr = b; 
◦ Element b[ n ] can be accessed by *( bPtr + n ) 
 Called pointer/offset notation 
◦ Addresses 
 &b[ 3 ] is same as bPtr + 3 
◦ Array name can be treated as pointer 
 b[ 3 ] is same as *( b + 3 ) 
◦ Pointers can be subscripted (pointer/subscript notation) 
 bPtr[ 3 ] is same as b[ 3 ]
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)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)

More Related Content

What's hot

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And Referencesverisan
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
Ali Aminian
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
Subhasis Nayak
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Pointers
PointersPointers
Pointers
sanya6900
 
pointers
pointerspointers
pointers
teach4uin
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
Rumman Ansari
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Prabhu Govind
 

What's hot (20)

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Pointers
PointersPointers
Pointers
 
pointers
pointerspointers
pointers
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Ponters
PontersPonters
Ponters
 
Pointer
PointerPointer
Pointer
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 

Viewers also liked

Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Blue Elephant Consulting
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
Vineeta Garg
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
Swarup Kumar Boro
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
Sasha Goldshtein
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
Aman Deep
 
SQL
SQLSQL
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 
stack & queue
stack & queuestack & queue
stack & queue
manju rani
 
GUI programming
GUI programmingGUI programming
GUI programming
Vineeta Garg
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
Vineeta Garg
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
hishamrizvi
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 

Viewers also liked (20)

Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...Intro To C++ - Class 11 - Converting between types, formatting floating point...
Intro To C++ - Class 11 - Converting between types, formatting floating point...
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
SQL
SQLSQL
SQL
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
stack & queue
stack & queuestack & queue
stack & queue
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board ExamsC++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
C++ Notes by Hisham Ahmed Rizvi for Class 12th Board Exams
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 

Similar to c++ pointers by Amir Hamza Khan (SZABISTIAN)

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.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
JoeyDelaCruz22
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
Rai University
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
Rai University
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
Pointers
PointersPointers
Pointers
Swarup Boro
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
Mehwish Mehmood
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
Abubakar524802
 
Pointers in c
Pointers in cPointers in c
Pointers in c
CHANDAN KUMAR
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
RashidFaridChishti
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 

Similar to c++ pointers by Amir Hamza Khan (SZABISTIAN) (20)

Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
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
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Object Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptxObject Oriented Programming using C++: Ch10 Pointers.pptx
Object Oriented Programming using C++: Ch10 Pointers.pptx
 
C language presentation
C language presentationC language presentation
C language presentation
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Recently uploaded

Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 

Recently uploaded (20)

Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 

c++ pointers by Amir Hamza Khan (SZABISTIAN)

  • 1.
  • 2. Name – Amir hamza khan Instructor –Sir Irfan Latif Memon Section – BSCS-1 RegNo. – 1312118
  • 3.  Introduction to pointers.  The addresss of operator “&”.  Pointers and the “*” operator.  Pointers and function parameters.  Size of operators. Relation between array and pointers.
  • 4.  The pointer is a variable that holds a memory address.  To understand the pointers we should first understand a bit about the computer memory.  Whenever any application is executed, it is loaded in to the memory form the disk and all the elements and components of that application will be located at the particular location somewhere in the memory.
  • 5.  Computer memory is divided into sequentially numbered memory locations and each variable is located at a unique location in memory, known as its address.  The address of these memory locations is typically represented in Hexadecimal format with the prefix 0x before the number (e.g. 0x8f4ffff2).
  • 6.  As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory  To return the address of the certain variables the address-of operator is placed in front of the variable name as,  cout<<&var1;  cout<<&var2;  cout<<&var3;  Now the “&” used with the variables in the cout statement will return the addresses at which these variable are located into the memory.
  • 7.  #include <iostream.h>  main ()  {  int var1;  char var2[10];  cout << "Address of var1 variable: ";  cout << &var1 << endl;  cout << "Address of var2 variable: ";  cout << &var2 << endl;  }
  • 8.  A pointer is a variable whose value is the address of another variable. Like any variable or constant, you must declare a pointer before you can work with it. The general form of a pointer variable declaration is:  Type *var-name;  Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
  • 9.  int *ip; // pointer to an integer  double *dp; // pointer to a double  float *fp; // pointer to a float  char *ch; // pointer to character  The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to
  • 10.  It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer
  • 11.  #include <iostream.h>  main ()  {  int *ptr = NULL;  Cout<<“the value of ptr is” <<ptr<<endl;   }
  • 12.  Example: Function to swap the values of two variables  #include <iostream.h>  void swap2(int* a, int* b)  {  int tmp;  tmp = *a;  *a = *b;  *b = tmp;  return;  }  int main()  {  int x = 1, y = 2;  swap2(&x, &y);  cout<<x<<y;  return 0;  }
  • 13.  Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.  We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap.  This is known as "call by value".
  • 14.  If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables.  The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.
  • 15.  The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.  The syntax of using sizeof is :  sizeof(data type)  The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type.  The sizeof operator can be used to get the size of classes, structures, unions and any other user defined data type.
  • 16. #include <iostream.h> main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; }
  • 17.
  • 18.  Arrays and pointers are closely related ◦ Array name is like constant pointer ◦ Pointers can do array subscripting operations
  • 19.  Accessing array elements with pointers ◦ Assume declarations: int b[ 5 ]; int *bPtr; bPtr = b; ◦ Element b[ n ] can be accessed by *( bPtr + n )  Called pointer/offset notation ◦ Addresses  &b[ 3 ] is same as bPtr + 3 ◦ Array name can be treated as pointer  b[ 3 ] is same as *( b + 3 ) ◦ Pointers can be subscripted (pointer/subscript notation)  bPtr[ 3 ] is same as b[ 3 ]