KEMBAR78
Learning C++ - Pointers in c++ 2 | PPTX
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

Learning C++ - Pointers in c++ 2

  • 1.
  • 2.
    Section Outline • Introductionto Memory • Pointers • Arrays • Scope 2
  • 3.
    Section Outline • Introductionto 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 • Introductionto Memory • Pointers • Arrays • Scope 6
  • 7.
    (this type canaccept 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() { voidvar1; 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 tothis 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 inta; 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 • Introductionto Memory • Pointers • Arrays • Scope 12
  • 13.
    • Definition ofan 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 isrelated 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 ofan 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 arrayand 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 • Introductionto Memory • Pointers • Arrays • Scope 20
  • 21.
    Scope • Every variablecan 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