Chapter Three-Ponters
Chapter Three-Ponters
Pointers
Injibara University
2015 E.C
1
Outline
Basic concept of pointers
2
Basic concept of pointers
A variable whose value is the address of another variable is known as pointer.
The address-of operator (&), can be used to get the memory address of an object.
Accessing the object to which a pointer points is called dereferencing the pointer.
The "*" operator, when it appears in an expression, dereferences the pointer to its right.
3
Cont.
Pointer variables are declared using the * (dereference operator).
int x = 5;
int *p = &x;
This means "p is a pointer to int. Its value is the address where x is stored"
4
How to read pointer
Reading an address directly is usually useless.
What we want to do is tell the program to "dereference" a pointer, that is, read the data the
pointer "points to".
The second line means "the value written in the address stored in p“.
5
Dereferencing a pointer
Generally, the * operator can be applied to a pointer to type T.
int x = 5;
int *p = &x;
*p = 6;
6
Pointers operators
(indirection/dereferencing operator)
Returns the value of what its operand points to
*yPtr returns y (because yPtr points to y).
* can be used to assign a value to a location in memory
*yptr = 7; // changes y to 7
Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are
inverses, cancel each other out
*&myVar == myVar and
&*yPtr == yPtr
7
Pointer expression, operation and arithmetic
int x, y, *p;
y = *(&x); /* same as y = x */
8
Cont.
Arithmetic operations on a pointer is the same as a numeric value.
The ++, --, +, and -are the main four types of arithmetic operators applied to the pointers.
Problem: write a program in C++ that finds the location of integer array with five elements.
Comparing pointers is possible using relational operators like ==,, and >.
P1 and P2 can be meaningfully compared if they both point to variables that are connected to
one another, such as the items of the same array.
9
Pointers and strings
Pointers are declared to point to a specific type and can point to objects of that type only.
A pointer
int* intptr; // intptr can store the address of an integer variable
char* charptr; // charptr can store the address of a character variable, or pointer for short, points to the
object whose address it stores.
10
Pointers and arrays
Array names are constant pointers
int a[10], *p, i;
p = a; /* p points to a[0] */
11
Cont.
It does not matter where you put the asterisk as long as it is between the type and the variable
name these three are equivalent: int* intptr; int * intptr; int *intptr;
In general, if T is some existing type, T* is the type of a variable that can store the address of
objects of type T, whether it is a simple scalar variable such as int, double, or char, or a more
structured variable such as a structure or a class:
Point* Pointptr;
12
Cont.
If the symbol "&" appears in a declaration, it is declaring that the variable to its right is a
reference variable, whereas if it appears in an expression that is not a declaration, it is the
address-of operator and its value is the address of the variable to its right.
int x = 972384; int y = 100; int &ref = x; // a declaration, so & is the reference operator int
*p = &x; // appears on right of =, so it is in an // expression and is address-of operator; p
stores address of x p = &y; // address-of operator; p stores the address of y int &z = y; //
reference operator; z is an alias for y
13
The relationship between array and pioneer
Arrays and pointers closely related.
14
Advantage of pointers
Pointers are useful in C++ for several reasons
15