fp 201 - unit 6

50
FUNDAMENTALS OF C+ + PROGRAMMING 6.0 POINTER (PENUDING)

Upload: rohassanie

Post on 10-Nov-2014

811 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: FP 201 - Unit 6

FUNDAMENTALS OF C++ PROGRAMMING

6.0 POINTER (PENUDING)

Page 2: FP 201 - Unit 6

6.1 Understand the concept of pointer

Page 3: FP 201 - Unit 6

Learning Outcome

At the end of the class, student should be able to: Define pointer and explain its function Declare pointer Explain new and delete operators

Page 4: FP 201 - Unit 6

What is pointer?

Pointer is the memory address of a variable.

Pointer is a variables that are used to store the addresses of other variables.

Page 5: FP 201 - Unit 6

What is pointer?

Memory Blocks

0x8f8dfff12 0x8f8dfff14 0x8f8dfff13

Address of Memory Blocks

Page 6: FP 201 - Unit 6

Declaring Pointer

Syntax:data_type *pointer_name;

Example double *p;

Page 7: FP 201 - Unit 6

The * and & operators

* operator Reference/dereference operator Produce the variable to which it point Refer to “value pointed by” Pointers are said to "point to"

Example of usage: double *p; // declare pointer to double variable void func(int *p) // declare p to be a pointer value

parameter

Page 8: FP 201 - Unit 6

The * and & operators

& operator Address-of operator Produce address of variable Example: double *p, v;

p = &v; // p point to address of v

Page 9: FP 201 - Unit 6

my_pointer = &my_variable

Pointer variables Address

operator

Memory location

Pointer Operator

int *my_pointer;int my_variable;my_pointer = &my_variable;

Page 10: FP 201 - Unit 6

How it will look like?

#include<iostream>using namespace std;void main(){int i;int *j;j = &i; //pointer to ii=4;

cout<<"i = " <<i<<endl;cout<<"j = " <<j<<endl;

}

Page 11: FP 201 - Unit 6

0012FF60

4

j = &i; //pointer to address ii=4;

i

j

Page 12: FP 201 - Unit 6

What is the output???

int i;

int *j;

j = &i; //pointer to i

i=4;

cout<<"i = " <<i<<endl;

cout<<"j = " <<*j<<endl;

Page 13: FP 201 - Unit 6

0012FF60

4

j = &i; //pointer to address ii=4;

i

j

cout<<"j = " <<*j

j 4

Page 14: FP 201 - Unit 6

How it will look like?

#include<iostream>using namespace std;void main(){

int *p1, v1;

v1 = 0;p1 = &v1; //pointer to v1*p1 = 42; //value pointed by p1 = 42

cout<<"v1="<<v1<<endl;cout<<"p1="<<*p1<<endl;

}

Output:

v1=42p1=42

Why?- As long as p1

contains address to v1, then both refer to the same variable

Page 15: FP 201 - Unit 6

v1 = 0;

&v1

0v1

p1

42

42p1

v1

p1 = &v1;

*p1 = 42; //value pointed by p1 = 42

Page 16: FP 201 - Unit 6

See difference?

9

8

p1 = p2;

p1

p2 9

8p1

p2

9

8p1

p2 9

9p1

p2

*p1 = *p2;

BEFORE AFTER

Page 17: FP 201 - Unit 6

Pointer Arithmetic

Pointer can only used two arithmetic operation addition and subtraction.

int a, int *p;p=&a;p=p+2;

What happen is: pointer move over bytes not adding 2 to value of a it point to the last 2 x int of the

integer a

Position of pointer p before operation

Position of pointer p after p+2 operation

Page 18: FP 201 - Unit 6

Pointer Arithmetic Example:#include<iostream>using namespace std;void main(){

int arraysize=4;int d []={1,2,3,4};for (int i = 0; i < arraysize; ++i)

cout << *(d + i) << " ";

}

its equivalent to:

#include<iostream>using namespace std;void main(){ int arraysize=4; int d []={1,2,3,4}; for (int i = 0; i < arraysize;

++i) cout << d[i] << " ";}

Page 19: FP 201 - Unit 6

Pointer & character string

Use standard function strcmp() uses two pointers to compare two strings:

strcmp (char *s1, char *s2){

while (*s1)if (*s1 - *s2) return *s1 - *s2; //if the string are not

equalelse {s1++;

s2++; }return 0;

}

Page 20: FP 201 - Unit 6

new and delete operator

new operator creates new dynamic variable of specific type

It also return a pointer that point to this new variable

Eg: int *n; n = new int(17); // initialize *n to 17

Page 21: FP 201 - Unit 6

How it will look like?

int *p1, *p2;

p1 = new int; *p1 = 42;

?p1

?p2

p1

?p2

p1

?p2

42

Page 22: FP 201 - Unit 6

p2 = p1;

*p2 = 53;

p2 = new int;

*p1 = 88;

p1

p2

p1

p2

42

53

p1

p2

p1

p2

53

?

53

88

Page 23: FP 201 - Unit 6

delete operator eliminates dynamic variable It also returns memory that the dynamic

variable occupied in freestore. Eg:

delete p; // p will be undefined

Good practice: Everytime you delete a pointer variable put it to

NULL to ensure it does not become dangling pointer!

Page 24: FP 201 - Unit 6

#include<iostream>

using namespace std;

void main()

{

int *p1;

p1 = new int;

*p1 = 10;

cout << "*p1 \t = "<< *p1 << endl;

delete p1;

cout << "*p1 \t = "<< *p1 << endl;

}

Page 25: FP 201 - Unit 6

In Class Exercise 1

What is the output?

int *p1, *p2;

p1 = new int;

p2 = new int;

*p1 = 10;

*p2 = 20;

cout << *p1 << "\t"<< *p2 << endl;

p1 = p2;

cout << *p1 << "\t"<< *p2 << endl;

*p1 = 30;

cout << *p1 << "\t"<< *p2 << endl;

*p1 = *p2;

cout << *p1 << "\t"<< *p2 << endl;

Page 26: FP 201 - Unit 6
Page 27: FP 201 - Unit 6

How it will look like?

int *p1, *p2;

p1 = new int; p2 = new int;

*p1 = 10; *p2 = 20;

?p1

?p2

p1

p2

p1

p2

10

20

Page 28: FP 201 - Unit 6

p1 = p2;

*p1 = 30;

*p1 = *p2;

p1

p2

p1

p2

p1

p2

10

30

30

30

10

20

Page 29: FP 201 - Unit 6

In Class Exercise 2

What is the different between the following variable?int *intPtr1, intPtr2;

Write a declaration for a pointer variable named my_new_ptr that points to dynamic variable of type double.

Page 30: FP 201 - Unit 6

6.2 Illustrate the relationship between pointer and array

Page 31: FP 201 - Unit 6

Learning Outcome

At the end of the class, student should be able to: Identify relationship between pointer and

array Write program using pointer and array

Page 32: FP 201 - Unit 6

Pointer & Array

Array is a collection of similar type of data Variable in an array can store memory address

instead of variable value Dynamic array is an array whose size is

determined during run-time. It is created using new operator. Eg:

double *new_array; //point to 1st index in the arraynew_array = new double[10] //to allocate storage

in memory

Page 33: FP 201 - Unit 6

Pointer in Array: Example 1

#include<iostream>

using namespace std;

void main()

{

int d []={1,2,3,4};

int *p1;

p1=d;

for (int i = 0; i < 4; ++i)

cout << *(p1 + i) << " ";

}

Page 34: FP 201 - Unit 6

Pointer in Array: Example 2

#include <iostream>using namespace std;void main() {

int array_size;cout << “Enter array size: “;cin >> array_size;int* my_dyn_array = new int [array_size];

cout << “Enter ” << array_size << “ number ” << endl;for (int i = 0; i < array_size; ++i)

cin >> my_dyn_array[i];}

Page 35: FP 201 - Unit 6

Pointer and Array

An array declared without specifying its size can be assume as a pointer to the address of the first element in the array.

Eg:int a[] = {2, 4, 6, 8, 10};

To iterate to the next element, add operation is used.

Page 36: FP 201 - Unit 6

Pointer and Array: Example 1#include <iostream>

using namespace std;

int main()

{

char susun[] = "Selamat belajar";

char *pt_str;

pt_str = susun; // point to first element

cout << "Turutan sblm perubahan " << susun;

cout << "\nSebelum perubahan, kandungan susun[5] : " <<susun[5];

*(pt_str+5) = 'C'; //change the 6th element

cout << "\nSelepas perubahan, kandungan susun[5] : " <<susun[5];

cout << "\nTurutan slps perubahan" << susun;

return 0;

}

Page 37: FP 201 - Unit 6
Page 38: FP 201 - Unit 6

Pointer and Array: Example 2#include <iostream>

using namespace std;

int main()

{

int senarai[] = {1,2,3,4,5};

int *point;

point=senarai; // point to first element

cout << "\nTurutan sblm perubahan " << senarai[0] << " " << senarai[1];

cout << " " << senarai[2] << " " << senarai[3] << " " << senarai[4];

cout << "\nSebelum perubahan, kandungan senarai[2] : " <<senarai[2];

*(point+2)= 7; // change the 3rd element

cout << "\nSelepas perubahan, kandungan senarai[2] : " <<senarai[2];

cout << "\nTurutan slps perubahan " << senarai[0] << " " << senarai[1];

cout << " " << senarai[2] << " " << senarai[3] << " " << senarai[4];

cout << endl;

return 0;

}

Page 39: FP 201 - Unit 6
Page 40: FP 201 - Unit 6

In Class Exercise 3

Write a declaration for pointer variable named char_ptr that will be used to point to a dynamic array of type char.

What is the output for the following code?int c[3] = {2, 3, 4};

int arraysize = 3, *p;

p = c;

for (int i = 0; i < arraysize; ++i)

cout << c[i] << “ “;

for (i = 0; i < arraysize; ++i)

cout << *(p + i) << “ “;

Page 41: FP 201 - Unit 6

Declarationchar * char_ptr; //point to 1st index in the array char_ptr = new char[6];

What is the output for the following code?

Page 42: FP 201 - Unit 6

6.3 Illustrate the relationship between pointer and function

Page 43: FP 201 - Unit 6

Learning Outcome

At the end of the class, student should be able to: Identify relationship between pointer and

function Use pointer as function argument Write program using pointer and function

Page 44: FP 201 - Unit 6

Pointer & Functions

2 types of function calls: Call by value Call by reference

Pointer can be used as parameters in both function

Page 45: FP 201 - Unit 6

Pointers & Function

#include <iostream>using namespace std;

void Change (int *, int *, int *);

void main(){int x=50, y=60, z=70;cout << "\nx = " << x << "\ny

= " << y << "\nz = " << z << "\n\n";

Change (&x, &y, &z);cout << "\nx = " << x << "\ny

= " << y << "\nz = " << z << "\n\n";

}

void Change (int *a, int *b, int *c){*a = 100;*b = 200;*c = 300;}

received address

change value in variable that is point by a,b,c

Page 46: FP 201 - Unit 6
Page 47: FP 201 - Unit 6

How it will look like?

Change(&x, &y, &z); void Change(int *a, int *b, int *c)

{*a = 100;*b = 200;*c = 300;}

50(100

)x

60(200

)y

70(300

)z

1002

1001

1003

*a

*b

*c

1001

1003

1002

*a

*b

*c

&x

&y

&z

Page 48: FP 201 - Unit 6

Pointers & Function

#include <iostream>using namespace std;

void Change (int &, int &, int &);

void main(){int x=50, y=60, z=70;cout << "\nx = " << x << "\ny

= " << y << "\nz = " << z << "\n\n";

Change (x, y, z);cout << "\nx = " << x << "\ny

= " << y << "\nz = " << z << "\n\n";

}

void Change (int &a, int &b, int &c){a = 100;b = 200;c = 300;}

received address

change value refered by a,b,c

Page 49: FP 201 - Unit 6
Page 50: FP 201 - Unit 6

Summary

Pointer is a memory address Pointer variable provides way to indirectly

name variable by naming the address of variable in memory

new and delete operator can be use to allocate and free the memory after pointer is used

Dynamic array is array whose sized is determined during runtime

Function can have pointer variable as its parameter variables.