You are on page 1of 20

10.

POINTERS
Programming Fundamentals
Spring 2019
Week 16
Outline

■ Introduction
■ Pointer
– Reference operator (&)
– Deference operator (*)
– Declaration of Pointer variables
■ Pointers and Arrays
■ Pointer Arithmetic
■ Pointer to Pointer
■ Pointer and Functions
■ Summed up -Course
10.1. Introduction

■ The memory of your computer can be imagined as a succession of memory cells,


each one of the minimal size that computers manage (one byte).
■ Every byte in the computer’s memory has an address
■ Addresses are numbers, just as they are for houses on a street
■ When a program is loaded into memory, it occupies a certain range of these
addresses.
■ We may be interested in knowing the address where our variable is being stored
during runtime in order to operate with relative positions to it.

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 3


Computer Memory
■ Each variable is assigned a memory slot (the size depends on the
data type) and the variable’s data is stored there

Memory address: 1020 1024 1032

… … 100 … 1024 …
a
Variable a’s value, i.e., 100, is
int a = 100; stored at memory location 1024
10.2 Pointers

■ The variable that stores the reference to another variable is called a pointer.
■ A pointer is a variable used to store the address of a memory cell.
■ We can use the pointer to reference this memory cell
int a = 100;
int *p;
Memory address: 1020 1024 1032

… … 100 … 1024 …
a p
integer
pointer
10.2.1 Reference Operator (&)
■ The "address of " operator (&) gives the memory
address of the variable
– Usage: &variable_name

Memory address: 1020 1024

… … 100 … … …
a
int a = 100; Output:
//get the value, 100
cout << a <<endl; //prints 100 1024
//get the memory address
cout << &a; //prints 1024
Reference Operator (&)

■ The address that locates a variable within memory is what we call a reference to
that variable.
■ This reference to a variable can be obtained by preceding the identifier of a variable
with an ampersand sign (&), known as reference operator,
■ The literal meaning of & is “ address of ". For example:
ted = &andy; //ted equals to address of andy

andy = 25;
fred = andy;
ted = &andy;

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 7


10.2.2 Dereference Operator (*)
■ Using a pointer we can directly access the value stored in the variable which it points to.
■ To do this, we simply have to precede the pointer's identifier with an asterisk (*), which
acts as dereference operator and that can be translated to " value pointed by ".
beth = *ted; //beth equals to value pointed by ted

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 8


Remember:

The difference between the reference and dereference operators:


■ & is the reference operator and can be read as "address of"
■ * is the dereference operator and can be read as "value pointed by"

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 9


10.2.3 Declaration of Pointer Variables
■ Declaration of Pointer variables
data_type *pointer_name;
where data_type can be int, char, double etc

int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
cout << p << " " << &p <<endl;

■ The value of pointer p is the address of variable a


■ A pointer is also a variable, so it has its own memory address
Example:

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 11


Don’t get confused
■ Declaring a pointer means only that it is a pointer: int *p;
■ Don’t be confused with the dereferencing operator, which is also written with an
asterisk (*). They are simply two different tasks represented with the same sign

int a = 100, b = 88, c = 8; 888


int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 is address of b
p2 = p1; // p2 equals to p1
b = *p3; // b equals to value pointed by p3 (p3 is address of c)
*p2 = *p3; // assign c to a
cout << a << b << c;
10.3. Pointers and Arrays

■ The concept of array is very much bound to the one of pointer. The identifier of an array
is equivalent to the address of its first element, supposing these two declarations:
■ The following assignment operation would be valid:
int numbers [20];
int * p;

■ p and numbers would be equivalent and would have the same properties.
■ Unlike p, which is an ordinary pointer, numbers is an array, and an array can be
considered a constant pointer. Therefore, the following allocation would not be valid:
p = numbers;  correct
■ Because numbers is an array, so it operates as a constant pointer, and we cannot assign
values to constants.
numbers = p;  wrong
Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 13
Check your understanding ??

Predict output ??

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 14


CYU:

10, 20, 30, 40, 50,

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 15


Pointer initialization

■ When declaring pointers we may want to explicitly specify which variable we want them to point to:
char *terry = “hello”;

■ It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702
indeed is the address of both of these.
■ Check point ??
output ??
cout << *(terry +4);
cout << terry [4];

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 16


10.4. Pointer Arithmetic
Given a pointer p, p+n refers to the element that is offset from p by
n positions.

a 2 p - 1

a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
10.5. Pointer to Pointer

What is the output?

58 58 58
10.6. Pointer and Functions
void IndirectSwap(char *Ptr1, char *Ptr2)
{
char temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main()
{
char a = 'y';
char b = 'n';
IndirectSwap(&a, &b);
cout << a << b << endl;
return 0;
}
All the studying
you are doing will
be worth it in the End !!
Good Luck

Instructor: Engr. Bushra Aijaz Electrical Engineering Dept, BUKC 21

You might also like