Post on 18-Jul-2016
description
1
Pointers Pointers (Lecture 7)(Lecture 7)
By:Dr. Norazah Yusof
FSKSM, UTM
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.
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
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
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
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
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;
}
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
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).
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
??
??
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
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;
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;
}
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
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
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
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…
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.
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;
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;
}
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
Exercises
■Answer Question 1 – 3 from Exercise 1 on page 65 & 66.
■Answer Question 1 – 5 from Exercise 2 on page 68 & 71.
22