Upload
aliaminian
View
307
Download
3
Embed Size (px)
Citation preview
1
Pointers
2
Section Outline
• Introduction to Memory• Pointers• Arrays• Scope
3
Section Outline
• Introduction to Memory• Pointers• Arrays• Scope
4
Introduction to Memory
• Problems related to pointer in C++ArraysFunction’s returnImportance of working with memories
Memory space (a cell) that has a value
.
.1000
1001
1002..
A Pointer
5
Introduction to Memory
• More Details:
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
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
Why initialization is important?
8
• e.g.int main(){
void var1;var1 = ‘A’;var1 = 1.322;float var2 = ( float ) var1;
}
Pointers
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
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.)
Return address
11
Pointers(cont.)
• Pointer`s access int a; int *ptr = a;
* ptr = 20;
*(ptr+1) = 23;
ptr++;
2023
XXXXXXXX
(1) 1001: ptr
(2) 1002: ptr
(3) 1003: ptr
12
Section Outline
• Introduction to Memory• Pointers• Arrays• Scope
13
• Definition of an array (literature)
• Dimensiono1D: has been exampled lattero2D : arrangement in memory
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 nameiii. Combining array and pointer conceptsiv. Array`s bound
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++ usuallyhas the zero number.
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++){ {
} }
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)
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;
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++){
}
myArray[counter] = counter; myPtr[counter]
= counter;A = B+1;
Arrays(cont.)
20
Section Outline
• Introduction to Memory• Pointers• Arrays• Scope
21
Scope
• Every variable can just appear and use exactly in a deterministic area that named “scope” .
• This rules over all the variable , even pointerse.g. int i = 0;if(myVar != 1){int i = 123;} cout << i; Out put is : 0
22
In future :
i. Functionsii. Class and related conceptsiii. ERRORS