learning c++ - pointers in c++ 2

22
Pointers 1

Upload: aliaminian

Post on 15-Jan-2017

304 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Learning C++ - Pointers in c++ 2

1

Pointers

Page 2: Learning C++ - Pointers in c++ 2

2

Section Outline

• Introduction to Memory• Pointers• Arrays• Scope

Page 3: Learning C++ - Pointers in c++ 2

3

Section Outline

• Introduction to Memory• Pointers• Arrays• Scope

Page 4: Learning C++ - Pointers in c++ 2

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

Page 5: Learning C++ - Pointers in c++ 2

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

Page 6: Learning C++ - Pointers in c++ 2

6

Section Outline

• Introduction to Memory• Pointers• Arrays• Scope

Page 7: Learning C++ - Pointers in c++ 2

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?

Page 8: Learning C++ - Pointers in c++ 2

8

• e.g.int main(){

void var1;var1 = ‘A’;var1 = 1.322;float var2 = ( float ) var1;

}

Pointers

Page 9: Learning C++ - Pointers in c++ 2

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

Page 10: Learning C++ - Pointers in c++ 2

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

Page 11: Learning C++ - Pointers in c++ 2

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

Page 12: Learning C++ - Pointers in c++ 2

12

Section Outline

• Introduction to Memory• Pointers• Arrays• Scope

Page 13: Learning C++ - Pointers in c++ 2

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

Page 14: Learning C++ - Pointers in c++ 2

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

Page 15: Learning C++ - Pointers in c++ 2

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.)

Page 16: Learning C++ - Pointers in c++ 2

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.)

Page 17: Learning C++ - Pointers in c++ 2

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.)

Page 18: Learning C++ - Pointers in c++ 2

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.)

Page 19: Learning C++ - Pointers in c++ 2

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.)

Page 20: Learning C++ - Pointers in c++ 2

20

Section Outline

• Introduction to Memory• Pointers• Arrays• Scope

Page 21: Learning C++ - Pointers in c++ 2

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

Page 22: Learning C++ - Pointers in c++ 2

22

In future :

i. Functionsii. Class and related conceptsiii. ERRORS