Sie sind auf Seite 1von 22

Pointers

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

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.

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 variables address
is the actual location in memory and only used by
the computer

Recall the Concept of Variables


Example:

The first variable uses name a, contains value 100 and located
at memory address 123456.

variable
name

content
123455
a

100

123456

123456

123460
123464
123465

address
4

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.
a 100
b

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

Variables address and its size


Example 2:
// This program uses the & operator to determine a
// variables 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;
}

Variables address and its size


Example 2:
// This program uses the & operator to determine a variables
// 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 0x0012ff88


The size of x is 4 bytes
The value in x is 25

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

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

10

Pointer variables
Example 3:
char Huruf;
int Nom;
char * x;
int * y;

Huruf = A;

Huruf
&Huruf A
&Huruf
x

Nom
&Nom 85
&Nom
y

Nom = 85;

x = &Huruf;
y = &Nom;
11

Initializing Pointer Variables

It is possible to initialize pointers when they are


declared and defined.
Example 4:
int x; //declare an integer variable x
int *p = &x;//declare a pointer to integer
//named p and set p to point to x at
//declaration time

int *p;
int *p = &x;
p = &x;
12

Store Variables 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 ptr1
cout << "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;
}

13

Store Variables address in Pointer


The
The
The
The

value in x
address of
address in
address in

is 25
x is 0x0012ff88
ptr1 is 0x0012ff88
ptr2 is 0x0012ff88

14

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

15

Accessing Variables through Pointers


Example 5:

Huruf

char Huruf, *x;


&Huruf A
&Nom
int Nom, *y;
Nom=9;
Huruf='A';
y = &Nom;
&Huruf
&Nom
x = &Huruf;
x
y
cout << *y << endl; //to access Nom thru y
cout << *x << endl; //to access Huruf thru x

Nom
9

16

Accessing Variables through Pointers


Example 5:

Huruf

char Huruf, *x;


&Huruf F
&Nom
int Nom, *y;
Nom=9;
Huruf='A';
y = &Nom;
&Huruf
&Nom
x = &Huruf;
x
y
cout << *y << endl; //to access Nom thru y
cout << *x << endl; //to access Huruf thru x

Nom
10

(*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
17

Self test 1
1.

Declare and define the following:


a.
b.
c.
d.
e.

A pointer variable pData1 pointing to a float.


A pointer variable pChar1 pointing to a character.
A pointer variable pChar1 pointing to an integer.
A pointer variable ppData1 pointing to a pointer to a float.
A pointer variable ppData1 pointing to a pointer to an integer.

18

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.
b.
c.
d.
e.

p
p
q
q
p

=
=
=
=
=

&x;
&y;
&x;
&y;
q;

19

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 pointers
ptr = &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;
}

20

Example 6: Indirection operator


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

21

Exercises

Answer Question 1 3 from


Exercise 1 on page 65 & 66.
Answer Question 1 5 from
Exercise 2 on page 68 & 71.

22

Das könnte Ihnen auch gefallen