22
1 Pointers Pointers (Lecture 7) (Lecture 7) By: Dr. Norazah Yusof FSKSM, UTM

TP2 Lect05 01 Pointers

Embed Size (px)

DESCRIPTION

pointers (c programming/c++)

Citation preview

Page 1: TP2 Lect05 01 Pointers

1

Pointers Pointers (Lecture 7)(Lecture 7)

By:Dr. Norazah Yusof

FSKSM, UTM

Page 2: TP2 Lect05 01 Pointers

2

Introduction■ A pointer is a derived data type.

– A data type built from one of the standard data type.■ Pointer variables contain memory address of a variable.

– Indirectly references a value.

Page 3: TP2 Lect05 01 Pointers

3

Recall the Concept of Variables■ A variable is a location in memory to hold data. ■ Each variable has an identifier name, a content,

and, an address. ■ The variable name is used by the programmer to

refer the memory location. The variable’s address is the actual location in memory and only used by the computer

Page 4: TP2 Lect05 01 Pointers

4

Recall the Concept of Variables■ Example: The first variable uses name a, contains value 100 and located

at memory address 123456.

address

100

123456

123456

123455

123460

123464

123465

a

b

variable name

content

Page 5: TP2 Lect05 01 Pointers

5

Pointer Variables■ A pointer variable is a special type of variable that

holds the address of another variable.■ Example: Variable b in is a pointer variable which is containing

the address of variable a.

b

100a

Page 6: TP2 Lect05 01 Pointers

6

The address operator■ & is the address operator

– Is used to get the address of a variable.– Provides a pointer constant to any named location in

memory.

■ Example 1: Print the address of an integer variable.int Nom;cout << &Nom;

Nom&Nom

Page 7: TP2 Lect05 01 Pointers

7

Variable’s address and its size■ Example 2:

// This program uses the & operator to determine a // variable’s address and the sizeof operator to// determine its size.#include <iostream>using namespace std;int main(){

int x = 25;cout << "The address of x is " << &x << endl;cout << "The size of x is " << sizeof(x) << " bytes\n";cout << "The value in x is " << x << endl;return 0;

}

Page 8: TP2 Lect05 01 Pointers

8

■ Example 2:// This program uses the & operator to determine a variable’s// address and the sizeof operator to determine its size.#include <iostream>using namespace std;int main(){

int x = 25;cout << "The address of x is " << &x << endl;cout << "The size of x is " << sizeof(x) << " bytes\n";cout << "The value in x is " << x << endl;return 0;

}

The address of x is 0x0012ff88The size of x is 4 bytesThe value in x is 25

Variable’s address and its size

Page 9: TP2 Lect05 01 Pointers

9

The Dereferencing operator■ The dereferencing operator, *, is used to get the

variable that a pointer variable is pointing to. ■ This operator can only be applied to pointer

variables (not to ordinary variables).

Page 10: TP2 Lect05 01 Pointers

10

Pointer variables■ Pointer variable stores the address of a variable.

■ Example 3:char Huruf;int Nom;char * x;int * y;

Huruf&Huruf

Nom&Nom

x

y

??

??

Page 11: TP2 Lect05 01 Pointers

11

Pointer variables■ Example 3:

char Huruf;int Nom;char * x;int * y;

Huruf = ‘A’;

Nom = 85;

x = &Huruf;

y = &Nom;

Huruf&Huruf A

Nom&Nom 85

x&Huruf

y&Nom

Page 12: TP2 Lect05 01 Pointers

12

Initializing Pointer Variables■ It is possible to initialize pointers when they are

declared and defined.■ Example 4:

int x; //declare an integer variable xint *p = &x;//declare a pointer to integer //named p and set p to point to x at //declaration time

int *p = &x;int *p;

p = &x;

Page 13: TP2 Lect05 01 Pointers

13

Store Variable’s address in Pointer

■ Example 5:// This program stores the address of a variable in a pointer.#include <iostream>using namespace std;int main(){

int x = 25, y=100; int *ptr1; // declaring pointer variable ptr1 int *ptr2 = &y; // declare and initialize pointer variable ptr2 // ptr2 is pointing to variable y

ptr1 = &x; // Assign value (Store the address of x) in ptr1cout << "The value in x is " << x << endl;cout << "The address of x is " << ptr1 << endl;ptr2 = ptr1; // Assign a pointer to another pointer

// Both pointers must be of the same type // ptr2 is now pointing to variable x

cout << "The address in ptr1 is " << ptr1 << endl;cout << "The address in ptr2 is " << ptr2 << endl;return 0;

}

Page 14: TP2 Lect05 01 Pointers

14

Store Variable’s address in Pointer

The value in x is 25The address of x is 0x0012ff88The address in ptr1 is 0x0012ff88The address in ptr2 is 0x0012ff88

Page 15: TP2 Lect05 01 Pointers

15

Accessing Variables Through Pointers■ To access to the pointed variable, use * as the

indirection operator.■ Various operations can be done using indirection

operator.– Assign value– Input/output operations– Arithmetic operations– Relational/logical expressions

Page 16: TP2 Lect05 01 Pointers

16

Accessing Variables through Pointers…

■ Example 5:char Huruf, *x;int Nom, *y;Nom=9;Huruf='A';y = &Nom;x = &Huruf;cout << *y << endl; //to access Nom thru ycout << *x << endl; //to access Huruf thru x

Huruf&Huruf A

Nom&Nom 9

x&Huruf

y&Nom

Page 17: TP2 Lect05 01 Pointers

17

■ Example 5:char Huruf, *x;int Nom, *y;Nom=9;Huruf='A';y = &Nom;x = &Huruf;cout << *y << endl; //to access Nom thru ycout << *x << endl; //to access Huruf thru x

Huruf&Huruf F

Nom&Nom 10

x&Huruf

y&Nom

(*y)++; //increment the value pointed by y

*x = 'F'; //assign the content pointed by x

cout << *y << endl; //display the content

cout << *x << endl; //display the content

Accessing Variables through Pointers…

Page 18: TP2 Lect05 01 Pointers

18

Self test 1

1. Declare and define the following:a. A pointer variable pData1 pointing to a float.b. A pointer variable pChar1 pointing to a character. c. A pointer variable pChar1 pointing to an integer. d. A pointer variable ppData1 pointing to a pointer to a float.e. A pointer variable ppData1 pointing to a pointer to an integer.

Page 19: TP2 Lect05 01 Pointers

19

Self test 2

2. Given the following declarations:int x;double y;int *p;double *q;

State whether the following expression is legal/illegal.a. p = &x; b. p = &y;c. q = &x; d. q = &y;e. p = q;

Page 20: TP2 Lect05 01 Pointers

20

Example 6: Indirection operator

// This program demonstrates the use of the indirection operator.#include <iostream>using namespace std;int main(){

int x = 25, y = 50, z = 75;int *ptr;cout << "Here are the values of x, y, and z:\n";cout << x << " " << y << " " << z << endl;

// Now the values in x, y and z are changed via pointersptr = &x; // Store the address of x in ptr.*ptr *= 2; // Multiply value in x by 2.ptr = &y; // Store the address of y in ptr.*ptr *= 2; // Multiply value in y by 2.ptr = &z; // Store the address of z in ptr.*ptr *= 2; // Multiply value in z by 2.cout << "Once again, here are the values of x, y, and z:\n";cout << x << " " << y << " " << z << endl;return 0;

}

Page 21: TP2 Lect05 01 Pointers

21

Here are the values of x, y, and z:25 50 75 Once again, here are the values of x, y, and z:\n";50 100 150

Example 6: Indirection operator

Page 22: TP2 Lect05 01 Pointers

Exercises

■Answer Question 1 – 3 from Exercise 1 on page 65 & 66.

■Answer Question 1 – 5 from Exercise 2 on page 68 & 71.

22