SlideShare a Scribd company logo
Pointers
1
Section Outline
• Introduction to Memory
• Pointers
• Arrays
• Scope
2
Section Outline
• Introduction to Memory
• Pointers
• Arrays
• Scope
3
Introduction to Memory
• Problems related to pointer in C++
Arrays
Function’s return
Importance of working with memories
4
Memory space (a cell)
that has a value
.
.
1000
1001
1002
.
.
A Pointer
Introduction to Memory
• More Details:
5
Memory space (a cell)
that has a value
.
.
1000
1001
1002
.
.
A Pointer 0 1 1 0 1 0 0 1
Section Outline
• Introduction to Memory
• Pointers
• Arrays
• Scope
6
(this type can accept any type in assignment
but when we want to assignment to other
variable we must do a cast)
• Definition of a Pointer in C++
Type * Name = NULL;
(it means ptrName points to nowhere , defines in <iostream.h>)
e.g.
int * ptr1;
void * ptr2
Pointers
7
Why initialization is
important?
• e.g.
int main()
{
void var1;
var1 = ‘A’;
var1 = 1.322;
float var2 = ( float ) var1;
}
Pointers
8
• Note :
– & has two means in two case
• Address fetch
int var1 ;
cout << & var1;
• In calling function
– * has two means in two case
• In definition of a variable, near the type means we are
defining a pointer
• Otherwise ,as a operator , get the value of a pointer
int a = 23;
int * pointer = a;
cout << *pointer;
Pointers
9
Out put : 23
• Notice to this code:
void main()
{
int var1 = 25;
int * var2 = & var1;
cout<<var1<<“n”<<var2<<“n”<<*var2;
}
Output: 25
0x9934344
25
Pointers(cont.)
10
Return address
Pointers(cont.)
• Pointer`s access
int a;
int *ptr = a;
* ptr = 20;
*(ptr+1) = 23;
ptr++;
11
20
23
XXXXXXXX
(1) 1001: ptr
(2) 1002: ptr
(3) 1003: ptr
Section Outline
• Introduction to Memory
• Pointers
• Arrays
• Scope
12
• Definition of an array (literature)
• Dimension
o1D: has been exampled latter
o2D : arrangement in memory
13
1001
1002
1003
0
3
6
1
4
8
2
5
7
Arrays
In C++
In Fortran
Arrays(cont.)
• What is related between array and pointers:
i. Definition of an array in C++
ii. Array`s name
iii. Combining array and pointer concepts
iv. Array`s bound
14
i. Definition of an array in C++
type Name[dimensions];
int Ar_name[5];
int Ar_name[10][20];
Note : if we define an array with 5 cell then C++ count them from
zero.
(for , while, do while)
 First things in C++ usually
has the zero number.
15
Cell No. 0
Cell No. 1
Cell No. 2
Cell No. 3
Arrays(cont.)
ii. Array`s name!
 array`s name has the address of the first cell in the memory.
int myArray[5];
int * myPtr = myArray;
int myArray[5]; int counter = 0;
int counter = 0;
for(; counter< 5; counter++) for(; counter< 5; counter++)
{ {
} }
16
myArray[counter]
= counter;
myPtr[counter]
= counter;
Arrays(cont.)
iii. Combining array and pointer concepts
define a pointer : int ptr[];
iv. Array`s bound
 In C++ we must be cautious about array boundaries(otherwise a
Exception appear)
17
0
1
2
1000(my_array)
1001
1002(my_array+2)
?????
Arrays(cont.)
• Initializing array
 It`s related to use to initializing an array or not , but it`s
better to initialize arrays, specially in calculation cases
 How to initialize an array
» int numbers[4] = {1, 3, 708, -12};
» float floats[3][2] = {{1.0,5.0},{0.5,3.1},{6,0.15}};
» char myArray [5] = {‘A’,’b’,’E’,’d’,’q’};
» Do this in a loop by initialize each after another
int myArray[10][5];
for(int i=0; i<10; i++)
for(int j=0; i<5; j++)
myArray[i][j] = 0;
18
Every where in C++ we can define a
variable
At some commands such as for, if, while,
do while the line just after them do not
need { } (or the first command after
these has impressed by themselves)
Arrays(cont.)
• Note:accolade
int myArray[5]; int myArray[5];
int counter = 0; int counter = 0;
for(; counter< 5; counter++) for(; counter< 5; counter++)
{
}
19
myArray[counter]
= counter; myPtr[counter]
= counter;
A = B+1;
Arrays(cont.)
Section Outline
• Introduction to Memory
• Pointers
• Arrays
• Scope
20
Scope
• Every variable can just appear and use exactly in a
deterministic area that named “scope” .
• This rules over all the variable , even pointers
e.g.
int i = 0;
if(myVar != 1)
{
int i = 123;
}
cout << i;
21
Out put is : 0
In future :
i. Functions
ii. Class and related concepts
iii.ERRORS
22
Ad

More Related Content

What's hot (20)

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
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Kamal Acharya
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
pointers
pointerspointers
pointers
teach4uin
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
Faisal Shahzad Khan
 
C pointers
C pointersC pointers
C pointers
Aravind Mohan
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
minal kumar soni
 
Pointers_c
Pointers_cPointers_c
Pointers_c
ahmed safwat
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
Software Systems and Graphic Designs
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Pointers
PointersPointers
Pointers
Swarup Kumar Boro
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
Hemantha Kulathilake
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
tech4us
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Prabhu Govind
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
웅식 전
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
 

Viewers also liked (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
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
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++
Ahmed Farag
 
Pointers
PointersPointers
Pointers
sarith divakar
 
Smart Pointer in C++
Smart Pointer in C++Smart Pointer in C++
Smart Pointer in C++
永泉 韩
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
Subhasis Nayak
 
Source-to-Source Compiler
Source-to-Source CompilerSource-to-Source Compiler
Source-to-Source Compiler
Mintoo Jakhmola
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
Shih Chi Lin
 
Anti aliasing
Anti aliasingAnti aliasing
Anti aliasing
Siwan Lama
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and EllipseComputer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse
2013901097
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
Taher Barodawala
 
06 clipping
06 clipping06 clipping
06 clipping
IMPECTRON
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
Shweta Patil
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
Drishti Bhalla
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
Shilpa Hait
 
Clipping
ClippingClipping
Clipping
johanna20
 
14 file handling
14 file handling14 file handling
14 file handling
APU
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
Mohamed El-Serngawy
 
intro to pointer C++
intro to  pointer C++intro to  pointer C++
intro to pointer C++
Ahmed Farag
 
Smart Pointer in C++
Smart Pointer in C++Smart Pointer in C++
Smart Pointer in C++
永泉 韩
 
Source-to-Source Compiler
Source-to-Source CompilerSource-to-Source Compiler
Source-to-Source Compiler
Mintoo Jakhmola
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
Shih Chi Lin
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and EllipseComputer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse
2013901097
 
Mid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer GraphicsMid point line Algorithm - Computer Graphics
Mid point line Algorithm - Computer Graphics
Drishti Bhalla
 
Cohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithmCohen-sutherland & liang-basky line clipping algorithm
Cohen-sutherland & liang-basky line clipping algorithm
Shilpa Hait
 
14 file handling
14 file handling14 file handling
14 file handling
APU
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
Mohamed El-Serngawy
 
Ad

Similar to Learning C++ - Pointers in c++ 2 (20)

Lec2
Lec2Lec2
Lec2
Ibrahim El-Torbany
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Ibrahim El-Torbany
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Advanced C - Part 3
Advanced C - Part 3Advanced C - Part 3
Advanced C - Part 3
Emertxe Information Technologies Pvt Ltd
 
Arrays
ArraysArrays
Arrays
Rahul Mahamuni
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
Vivek Bhargav
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp full
Võ Hòa
 
Lec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structureLec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structure
bhargavi804095
 
05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Chp4(ref dynamic)
Chp4(ref dynamic)Chp4(ref dynamic)
Chp4(ref dynamic)
Mohd Effandi
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
Education Front
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
Sabaunnisa3
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Dynamic Memory Allocation.pptx for c language and basic knowledge.
Dynamic Memory Allocation.pptx for c language and basic knowledge.Dynamic Memory Allocation.pptx for c language and basic knowledge.
Dynamic Memory Allocation.pptx for c language and basic knowledge.
2024163103shubham
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
Vivek Bhargav
 
Getting started cpp full
Getting started cpp   fullGetting started cpp   full
Getting started cpp full
Võ Hòa
 
Lec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structureLec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structure
bhargavi804095
 
05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf05_Arrays C plus Programming language22.pdf
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
Yandex may 2013 a san-tsan_msan
Yandex may 2013   a san-tsan_msanYandex may 2013   a san-tsan_msan
Yandex may 2013 a san-tsan_msan
Yandex
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Dynamic Memory Allocation.pptx for c language and basic knowledge.
Dynamic Memory Allocation.pptx for c language and basic knowledge.Dynamic Memory Allocation.pptx for c language and basic knowledge.
Dynamic Memory Allocation.pptx for c language and basic knowledge.
2024163103shubham
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
Saad Gabr
 
Ad

Recently uploaded (20)

Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Physical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A ReviewPhysical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A Review
Journal of Soft Computing in Civil Engineering
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...
Diego López-de-Ipiña González-de-Artaza
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
OPTIMIZING DATA INTEROPERABILITY IN AGILE ORGANIZATIONS: INTEGRATING NONAKA’S...
ijdmsjournal
 
Deepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber ThreatsDeepfake Phishing: A New Frontier in Cyber Threats
Deepfake Phishing: A New Frontier in Cyber Threats
RaviKumar256934
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Environment .................................
Environment .................................Environment .................................
Environment .................................
shadyozq9
 
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdfIBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
IBAAS 2023 Series_Lecture 8- Dr. Nandi.pdf
VigneshPalaniappanM
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
vtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdfvtc2018fall_otfs_tutorial_presentation_1.pdf
vtc2018fall_otfs_tutorial_presentation_1.pdf
RaghavaGD1
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...Citizen Observatories to encourage more democratic data evidence-based decisi...
Citizen Observatories to encourage more democratic data evidence-based decisi...
Diego López-de-Ipiña González-de-Artaza
 
AI Chatbots & Software Development Teams
AI Chatbots & Software Development TeamsAI Chatbots & Software Development Teams
AI Chatbots & Software Development Teams
Joe Krall
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 

Learning C++ - Pointers in c++ 2

  • 2. Section Outline • Introduction to Memory • Pointers • Arrays • Scope 2
  • 3. Section Outline • Introduction to Memory • Pointers • Arrays • Scope 3
  • 4. Introduction to Memory • Problems related to pointer in C++ Arrays Function’s return Importance of working with memories 4 Memory space (a cell) that has a value . . 1000 1001 1002 . . A Pointer
  • 5. Introduction to Memory • More Details: 5 Memory space (a cell) that has a value . . 1000 1001 1002 . . A Pointer 0 1 1 0 1 0 0 1
  • 6. Section Outline • Introduction to Memory • Pointers • Arrays • Scope 6
  • 7. (this type can accept any type in assignment but when we want to assignment to other variable we must do a cast) • Definition of a Pointer in C++ Type * Name = NULL; (it means ptrName points to nowhere , defines in <iostream.h>) e.g. int * ptr1; void * ptr2 Pointers 7 Why initialization is important?
  • 8. • e.g. int main() { void var1; var1 = ‘A’; var1 = 1.322; float var2 = ( float ) var1; } Pointers 8
  • 9. • Note : – & has two means in two case • Address fetch int var1 ; cout << & var1; • In calling function – * has two means in two case • In definition of a variable, near the type means we are defining a pointer • Otherwise ,as a operator , get the value of a pointer int a = 23; int * pointer = a; cout << *pointer; Pointers 9 Out put : 23
  • 10. • Notice to this code: void main() { int var1 = 25; int * var2 = & var1; cout<<var1<<“n”<<var2<<“n”<<*var2; } Output: 25 0x9934344 25 Pointers(cont.) 10 Return address
  • 11. Pointers(cont.) • Pointer`s access int a; int *ptr = a; * ptr = 20; *(ptr+1) = 23; ptr++; 11 20 23 XXXXXXXX (1) 1001: ptr (2) 1002: ptr (3) 1003: ptr
  • 12. Section Outline • Introduction to Memory • Pointers • Arrays • Scope 12
  • 13. • Definition of an array (literature) • Dimension o1D: has been exampled latter o2D : arrangement in memory 13 1001 1002 1003 0 3 6 1 4 8 2 5 7 Arrays In C++ In Fortran
  • 14. Arrays(cont.) • What is related between array and pointers: i. Definition of an array in C++ ii. Array`s name iii. Combining array and pointer concepts iv. Array`s bound 14
  • 15. i. Definition of an array in C++ type Name[dimensions]; int Ar_name[5]; int Ar_name[10][20]; Note : if we define an array with 5 cell then C++ count them from zero. (for , while, do while)  First things in C++ usually has the zero number. 15 Cell No. 0 Cell No. 1 Cell No. 2 Cell No. 3 Arrays(cont.)
  • 16. ii. Array`s name!  array`s name has the address of the first cell in the memory. int myArray[5]; int * myPtr = myArray; int myArray[5]; int counter = 0; int counter = 0; for(; counter< 5; counter++) for(; counter< 5; counter++) { { } } 16 myArray[counter] = counter; myPtr[counter] = counter; Arrays(cont.)
  • 17. iii. Combining array and pointer concepts define a pointer : int ptr[]; iv. Array`s bound  In C++ we must be cautious about array boundaries(otherwise a Exception appear) 17 0 1 2 1000(my_array) 1001 1002(my_array+2) ????? Arrays(cont.)
  • 18. • Initializing array  It`s related to use to initializing an array or not , but it`s better to initialize arrays, specially in calculation cases  How to initialize an array » int numbers[4] = {1, 3, 708, -12}; » float floats[3][2] = {{1.0,5.0},{0.5,3.1},{6,0.15}}; » char myArray [5] = {‘A’,’b’,’E’,’d’,’q’}; » Do this in a loop by initialize each after another int myArray[10][5]; for(int i=0; i<10; i++) for(int j=0; i<5; j++) myArray[i][j] = 0; 18 Every where in C++ we can define a variable At some commands such as for, if, while, do while the line just after them do not need { } (or the first command after these has impressed by themselves) Arrays(cont.)
  • 19. • Note:accolade int myArray[5]; int myArray[5]; int counter = 0; int counter = 0; for(; counter< 5; counter++) for(; counter< 5; counter++) { } 19 myArray[counter] = counter; myPtr[counter] = counter; A = B+1; Arrays(cont.)
  • 20. Section Outline • Introduction to Memory • Pointers • Arrays • Scope 20
  • 21. Scope • Every variable can just appear and use exactly in a deterministic area that named “scope” . • This rules over all the variable , even pointers e.g. int i = 0; if(myVar != 1) { int i = 123; } cout << i; 21 Out put is : 0
  • 22. In future : i. Functions ii. Class and related concepts iii.ERRORS 22
  翻译: