0% found this document useful (0 votes)
16 views10 pages

L 19 Pointers

The document explains the concept of pointers in C++, detailing their declaration, usage, and operations. It covers how pointers reference memory addresses, the distinction between reference and dereference, and the implications of using constant pointers and pointers to constants. Additionally, it discusses pointer arithmetic, the null pointer problem, and how pointers can be used with objects.

Uploaded by

anouraissa63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

L 19 Pointers

The document explains the concept of pointers in C++, detailing their declaration, usage, and operations. It covers how pointers reference memory addresses, the distinction between reference and dereference, and the implications of using constant pointers and pointers to constants. Additionally, it discusses pointer arithmetic, the null pointer problem, and how pointers can be used with objects.

Uploaded by

anouraissa63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Pointers

What Is Pointer
every variable has memory address
char c=’y’; name memory address
int i=2; c ’y’ 0021
address of variable i is 0022 i 2 0022
address can used to refer to this variable
address can be stored in a variable of special type
called pointer (variable)
cp 0021
C++ provides an abstraction of pointer
pointer is used only to refer to the variable it
points to - we usually don’t think of pointers as
holding integer (address) just a reference to a name memory address
variable c ’y’ 0021
pointer: a mechanism to uniformly manipulate
multiple memory locations in sequence i 2 0022

cp

2
Pointer Declaration
pointer variable is declared as follows:
typeOfVariablePointedTo *pointerName;

example:
double *p;
int *ip;
pointer declarations can be intermixed with ordinary variable declarations:
char *cp, c1=’y’, c2=’n’;
int i, *ip;
star can move to type without changing semantics:
int *i, j; is the same as int* i, j;

pointer to a pointer is legal and sometimes used: char **cpp;

3
Reference and Dereference

& reference or address of operator: returns the address of a variable, used


to assign value to pointer
cp = &c1; // until reassigned cp ”points to” c1

* dereference or indirection operator: access the location the pointer


points to, two forms
for reading: cout << *cp << endl;
for writing: *cp = ’G’;

note that star at pointer declaration is not a dereference operator – it just


signifies that the variable is a pointer.

4
Pointer Usage
pointer can be initialized
char *cp2=&c2;
int *ip;

pointer can point to multiple variables (in sequence) and multiple pointers can
point to the same variable

what does this code fragment do?


int *ip1, *ip2, one=1, two=2;
ip1=&one;
ip2=ip1;
*ip1 = *ip1 + 1;
ip1=&two;
*ip1 -= 1;
cout << *ip2 << ” ” << *ip1;

5
Constants and Pointers
constant pointer: cannot change where pointer points to (can modify value
in the location)
char c = 'c';
const char d = 'd';
char *const ptr1 = &c;
ptr1 = &d; // illegal
pointer to a constant: cannot change what pointer points to (can make
pointer point elsewhere)
const char *ptr2 = &d;
*ptr2 = 'e'; // illegal: cannot change d
// through dereferencing ptr2
this also declares a pointer to a constant
char const *ptr2 = &d;
to recognize type, read from right to left

6
Array Names and Constant Pointers
array name is in fact a constant pointer
example
int *p; // this is a pointer
int a[5]; // this is an array
// int *const a; plus memory allocation
// is equivalent
p = a; // now pointer references first
// element of an array
an array name can be used as name and as pointer:
a[3]=22; // as array name: applying indexing
p = a; // as pointer
a pointer can also be used similarly
p[4]=44; // as name
p = a; // as pointer
since array name is a constant pointer – its modification is illegal
a=p; // ERROR!

7
Pointer Arithmetic
array elements are guaranteed to be in continuous memory locations
adding one to pointer value advances it one memory location of its specified type
int a[5], *p = a;
p = p + 1; // p points to second element of the array
gives alternative way to manipulate arrays
allowed pointer operations: add/subtract integer, compound assignment,
increment, decrement, subtract two pointers of the same type (what’s the purpose of
that?)
++p; // moves p one position to the right – points to
// third element of array
p -=2; // moves p two positions to the left
cout << p – a; // prints how many elements between p and a
other arithmetic operations, like pointer division or multiplication, are not allowed
regular and pointer arithmetic operations can be intermixed
*(++p) = 22; // what does this do?
caution
use only on continuous memory locations
terse but obscure
– indexing may be clearer to understand
– error prone
8
Null Pointer/Loose Pointer Problem
a pointer that is not initialized holds arbitrary value
assigning a value to the location uninitialized pointer points to can lead to
unpredictable results: loose (dangling) pointer problem
int *ptr;
*ptr = 5; // ERROR - loose pointer!
what is the result of this assignment?
nullptr constant guaranteed to be different from any address in memory
convenient for pointer initialization
int *ptr = nullptr;
assigning nullptr to pointer does not eliminate loose pointer problem,
useful to check if pointer is initialized
int *ptr2 = nullptr, i=5;
*ptr2 = 5; // ERROR - still loose
if (ptr2 == nullptr) // is it initialized?
ptr2=&i;
cout << *ptr2;

9
Pointers to Objects
pointers can point to objects:
myclass{
public:
void setd(int i){d=i;};
int getd() const {return d;};
private:
int d;
};
myclass ob1, *obp=&ob1;
members can be accessed using pointers:
(*obp).setd(5);
parentheses around (*obp) are needed because dot-operator has higher
precedence than dereferencing
a shorthand -> is used for accessing members of the object the pointer points to:
cout << obp->getd();

10

You might also like