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

More Related Content

PPTX
Pointer in C++
PPTX
C programming - Pointers
PPT
Pointers
PPT
Pointers+(2)
PDF
PPTX
Pointers in c++
PPTX
Used of Pointer in C++ Programming
PPT
Pointers in C
Pointer in C++
C programming - Pointers
Pointers
Pointers+(2)
Pointers in c++
Used of Pointer in C++ Programming
Pointers in C

What's hot (20)

PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPTX
Pointers in C
PPTX
Pointer in c
PPT
pointers
PPTX
Pointers in C/C++ Programming
PPT
C pointers
ODP
Pointers in c++ by minal
PDF
Pointers_c
PPTX
Pointers in c++
PDF
PPT
detailed information about Pointers in c language
PDF
C Pointers
PPTX
COM1407: Working with Pointers
PPT
Pointers (Pp Tminimizer)
PPTX
Pointer in C
PPT
Pointers in C
PPTX
This pointer
PDF
10. array & pointer
PDF
Pointers & References in C++
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Pointers in C
Pointer in c
pointers
Pointers in C/C++ Programming
C pointers
Pointers in c++ by minal
Pointers_c
Pointers in c++
detailed information about Pointers in c language
C Pointers
COM1407: Working with Pointers
Pointers (Pp Tminimizer)
Pointer in C
Pointers in C
This pointer
10. array & pointer
Pointers & References in C++
Ad

Viewers also liked (20)

PPTX
Pointers in c++
PDF
Pointer in c++ part1
PPTX
C++ Pointers
PPT
Unit 6 pointers
PPTX
intro to pointer C++
PDF
Pointers
PPT
Smart Pointer in C++
PDF
Pointer in c++ part2
DOC
Source-to-Source Compiler
PPTX
Intro to C++ Basic
PPTX
Anti aliasing
PPTX
Computer Graphic - Lines, Circles and Ellipse
PPT
Polygon clipping
PPT
06 clipping
PPTX
Computer graphics
PPTX
Mid point line Algorithm - Computer Graphics
PPT
Cohen-sutherland & liang-basky line clipping algorithm
PDF
Clipping
PPT
14 file handling
 
PPTX
Circle & curve clipping algorithm
Pointers in c++
Pointer in c++ part1
C++ Pointers
Unit 6 pointers
intro to pointer C++
Pointers
Smart Pointer in C++
Pointer in c++ part2
Source-to-Source Compiler
Intro to C++ Basic
Anti aliasing
Computer Graphic - Lines, Circles and Ellipse
Polygon clipping
06 clipping
Computer graphics
Mid point line Algorithm - Computer Graphics
Cohen-sutherland & liang-basky line clipping algorithm
Clipping
14 file handling
 
Circle & curve clipping algorithm
Ad

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

PPTX
arraytypes of array and pointer string in c++.pptx
PPTX
c++ arrays and pointers grade 9 STEP curriculum.pptx
PPTX
Array In C++ programming object oriented programming
PDF
C++ computer language chapter 4 pointers.pdf
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPTX
POLITEKNIK MALAYSIA
PPT
Pointer
PDF
4.ArraysInC.pdf
PPTX
Array in c++
PPTX
20.C++Pointer.pptx
PPT
CHAPTER-5.ppt
PPT
PDF
PPTX
Chapter 3 summaryv2(Pointers) in c++.pptx
PPTX
CSCI 238 Chapter 08 Arrays Textbook Slides
PPTX
Lecture 9
PPTX
Data structure array
PPTX
Pointers in C++ object oriented programming
PPTX
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
arraytypes of array and pointer string in c++.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
Array In C++ programming object oriented programming
C++ computer language chapter 4 pointers.pdf
C++ - UNIT_-_IV.pptx which contains details about Pointers
week14Pointers_II. pointers pemrograman dasar C++.pptx
POLITEKNIK MALAYSIA
Pointer
4.ArraysInC.pdf
Array in c++
20.C++Pointer.pptx
CHAPTER-5.ppt
Chapter 3 summaryv2(Pointers) in c++.pptx
CSCI 238 Chapter 08 Arrays Textbook Slides
Lecture 9
Data structure array
Pointers in C++ object oriented programming
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE

Recently uploaded (20)

PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Construction Project Organization Group 2.pptx
PPT
Project quality management in manufacturing
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Digital Logic Computer Design lecture notes
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
OOP with Java - Java Introduction (Basics)
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Construction Project Organization Group 2.pptx
Project quality management in manufacturing
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CYBER-CRIMES AND SECURITY A guide to understanding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Internet of Things (IOT) - A guide to understanding
CH1 Production IntroductoryConcepts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT 4 Total Quality Management .pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Digital Logic Computer Design lecture notes
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Foundation to blockchain - A guide to Blockchain Tech
OOP with Java - Java Introduction (Basics)

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