Chapter Four: Pointers

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

Chapter Four

Pointers
Pointer
• A pointer is a variable that holds the address of
something else.
• Syntax :
dataType *identifier;
e.g.
int *x;
x is a pointer to an integer.
• Addresses: garbage, something, nothing
– When created:
int * q;
q = 0; // now it points to nothing
q = NULL; // not portable, use 0 instead
Pointers to anything
x some int
int *x;
z some double
double *z;

int **y; y some *int some int


• To assign a value to the pointer variable
– use the address operator &
• E.g. Address
MEMORY

int foo; 0
1
int *x; 2
foo 3 123
4
foo = 123; 5
x = &foo;

...

...
x 81345 3
81346
81347
&foo
In C++ you can get the address of a
variable with the “&” operator.

int foo; MEMORY


Address
0
1
foo = 123; 2
x = &foo; foo 3 123
4
5
• &foo means “the address of foo”

...

...
• Must always refer to something
– Must be initialized, cannot be changed
Dereferencing operator
• To assign a value to the variable foo through the
pointer variable
– we must use the dereferencing operator *
– E.g
*x=10;
//Changes the vale of foo from 123 to 10
Equivalent to
foo=10;
• You can use the integer x points to in a C++
expression like this:
y = *x + 17;

*x = *x +1;
#include <iostream.h>
int main() {
int *ptr_a, *ptr_b;
int num_c = 4, num_d = 7;
ptr_a = &num_c;
ptr_b = ptr_a;
cout << *ptr_a << " " << *ptr_b << "\n";
ptr_b = &num_d;
cout << *ptr_a << " " << *ptr_b << "\n";
*ptr_a = *ptr_b;
cout << *ptr_a << " " << *ptr_b << "\n";
cout << num_c << " " <<
*&*&*&num_c << "\n";
return 0; }
• The output of this program is:
• 44
• 47
• 77
• 77
More Pointers
• int i = 50;
int j = 75;
int *p1 ; int * p2
p1 = &i ; p2 = & j;
cout << *p1;
p1 = p2 ; *p2 =0;
cout <<*p1;
Assigning a value to a
dereferenced pointer
A pointer must have a value before you can
dereference it (follow the pointer).

int *x; int foo;


*x=3; int *x;
x = &foo;
*x=3;
Location and Value Comparisons
• Pointers may be compared for equality
– Same as comparing addresses of what pointers
point to (memory locations:)
– Contents of what pointers point to may be
compared, too
• First implies second, not other way around
int *p
5 0xefffdad0 p == q &*p == &*q
int i int *q
5 0xefffdbd0
*p == *q
int j
Const Pointers and Pointers to Const
Types
int main ( ){
• Make promises via the const
const int i = 0; keyword in pointer declaration:
int j = 1; – not to change pointer itself
int k = 2;
– not to change value it aliases
// pointer to int – can also promise neither/both
int * w = &j;
• Read declarations right to left
// const pointer to int • In this example, can change
int * const x = &k;
– w and what it points to
// pointer to const int – what x points to but not x
const int * y = &i; – y but not what it points to
// const pointer to const int – neither z nor what it points to
const int * const z = &j; • A pointer to a non-const type
}
cannot point to a const variable
– w and x can’t point to i
Pointers and arrays
• The name of an array is a pointer to the
0th element of the array (the beginning of
the array)
int array[5] ;
// array is equivalent to & array[0]
*array = 5; is like array[0] = 5;
int j = *(array+4) is like int j = array[4]
cout << *array; is like cout << array[0];
Pointers and Arrays
• An array name is basically a const pointer.
• You can use the [] operator with a pointer:

int *x;
int a[10];
x = &a[2]; x is “the address of a[2] ”
for (int i=0;i<3;i++)
x[i]++;

x[i] is the same as a[i+2]


Pointers
• Summary
– &p, p, and *p all have different meanings
– &p means the address of p
– p means the content of p
– *p means the content pointed to by p, that is
pointed to by the content of memory location
Pointer Arithmetic
• can add an integer quantity to or subtract
an integer quantity from a pointer
• the outcome depends on the size of the
object pointed to
• not the same as integer arithmetic
Pointer arithmetic
• Integer math operations can be used with
pointers.
• If you increment a pointer, it will be
increased by the size of whatever it points
to.
int *ptr = a;
*(ptr+2)
*(ptr+4)
*ptr

a[0] a[1] a[2] a[3] a[4]

int a[5];
printing an array
void print_array(int a[], int len) {
for (int i=0;i<len;i++)
cout << "[" << i << "] = "
<< a[i] << endl;
}

void print_array(int *a, int len) {


for (int i=0;i<len;i++)
cout << "[" << i << "] = "
<< *a++ << endl;
}
Dynamic memory
• Memory for dynamic objects
– Requested from the free store
• Free store is memory controlled by operating
system
Operation specifies
 The type and number of objects

If there is sufficient memory to satisfy the request


 A pointer to sufficient memory is returned by the operation

If there is insufficient memory to satisfy the request


 An exception is generated

 An exception is an error state/condition which if not


handled (corrected) causes the program to terminate
The Basic New Form
• Syntax
Ptr = new SomeType ;
– Where
• Ptr is a pointer of type SomeType

• Beware
– The newly acquired memory is uninitialized
unless there is a default SomeType
constructor
Examples
int *iptr = new int;
Rational *rptr = new Rational;
Uninitialized int object

iptr —

rptr 0/1

Rational object with default


initialization

int *iptr = new int(10);


Rational *rptr = new Rational(1,2);
i pt r 10

rptr 1/2
The Primary New Form
• Syntax
P = new SomeType [Expression] ;
– Where
• P is a pointer of type SomeType
• Expression is the number of contiguous objects of type
SomeType to be constructed -- we are making a list

– Note
• The newly acquired list is initialized if there is a default
SomeType constructor

• Because of flexible pointer syntax


– P can be considered to be an array
Examples
int *A = new int [3];
Rational *R = new Rational[2];
A[1] = 5;
Rational r(2/3);
R[0] = r;
A — 5 —

R 2/3 0/1
Delete Operators
• Forms of request
delete P; // used if storage
came from new
delete [] P; // used if storage
came from new[]
– Storage pointed to by P is returned to free
store
• P is now undefined
Cleaning Up
int n;
cout << "Enter list size: ";
cin >> n;
int *A = new int[n];
GetList(A, n);
SelectionSort(A, n);
DisplayList(A, n);
delete [] A;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy