You are on page 1of 23

Fundamentals of Computer Science

Lecture 22: Pointers and Arrays


Ethan Cerami
New York University
Road-Map
• Pointer Arithmetic
– Data Types (Review)
– Pointing to Arrays
– Pointer Arithmetic
• Pointers and Arrays
– Using Array Notation
– Using Pointer Offset notation

• Reading: Chapter 7: Sections 7.7- 7.8


Pointer Arithmetic
Data Types (Review)
• In order to understand pointer arithmetic, we have
to quickly review how data types work.
• You may recall:
– all variables take up a certain number of bytes
in memory.
– each data type gets a specific number of bytes
(for example, doubles usually have twice as
many bytes as ints.)
– because variables have a finite set of bytes, they
therefore have a finite number range
• hence, positive integers can wrap around and
become negative numbers.
Date Types
• Memory allocation is always dependent on the
operating system.
• On our system, we have the following:
– int 4 bytes Range: -32,768 to +32,767
– long 8 bytes Range: - ~ 2 billion to + ~2 billion
– float 4 bytes Range: ±3.4 x 10-38 to ± 3.4 x 10+38
– double 8 bytes Range: ±1.7 x 10-4932 to ± 1.7 x 10+4932
• To summarize:
– longs take up more memory than ints
– doubles take up more memory than floats
Pointing to Arrays
• So far, we have seen that pointers can point to
other variables.
• For example:
int x = 5;
int *xPtr;
xPtr = &x;

xPtr x
Pointing to Arrays
• Pointers can also point to arrays.
• For example:
int x[5] = {2,4,6,8,10};
int *xPtr = NULL;
• When you declare x, the operating system will
allocate space for you array:
– Each element in the array will take up 4 bytes of memory
(see previous slides.) The operating system therefore needs
to allocate (5 elements * 4 bytes = 20 bytes of memory.)
– Let’s assume that the operating system finds space at
memory location 100.
Pointing to Arrays
• The x array in memory:
Array
Indexes
[0] [1] [2] [3] [4]

Values 2 4 6 8 10

Physical
Addresses 100 104 108 112 116

• integers take up 4 bytes each. Hence:


– x[0] is located at address 100
– x[1] is located at address 104
– x[2] is located at address 108
Pointing to Arrays
• Now we have the following code:
xPtr = &x[0]
xPtr will now point to the zeroeth element in the array

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

2 4 6 8 10

xPtr
100 104 108 112 116
• The value of xPtr is now: 100
• The value of *xPtr is now: 2
Pointing to Arrays
• Now we have the following code:
xPtr = &x[4]
xPtr will now point to element #4 in the array

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

2 4 6 8 10

xPtr
100 104 108 112 116
• The value of xPtr is now: 116
• The value of *xPtr is now: 10
Pointing to Arrays
• Shortcut: an array name is an alias for the address
of the 0th element in the array.
xPtr = x;
is the same as: xPtr = &x[0]

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

2 4 6 8 10

xPtr
100 104 108 112 116
Pointer Arithmetic
• Once you have a pointer, you can apply a number
of arithmetic operators to it:
++ Increment
-- Decrement
+ Plus
- Minus
• Suppose you have xPtr pointing to element 0 of an
array, and then suppose you want to print each
element in the array:
– You can use pointer arithmetic to do this!
Pointer Arithmetic
• Pointer Arithmetic is not the same as regular arithmetic.
• For example, suppose we have the following code:

int x = 5;
x++;

After this, x is (obviously) set to 6 (no mystery there!)


• However, suppose we have:

int x[5] = {2,4,6,8,10};


int *xPtr = NULL;
xPtr = &x; (xPtr is set to 100)
xPtr++; (this does not set xPtr to 101!)
(it actually sets xPtr to 104)
(moves xPtr to point to next element in the array.)
Pointer Arithmetic
• Pointer arithmetic depends on the data type.
• Integers take up 4 bytes.
– Therefore, if you increment a pointer to an int, you actually
increment by four bytes.
– If you increment a pointer to a double, you actually increment by
eight bytes.
• Another example:

int x[5] = {2,4,6,8,10};


int *xPtr = NULL;
xPtr = &x; (xPtr is set to 100)
xPtr+=2; (again, does not set xPtr to 102.)
(it actually sets xPtr to 108.)
(xPtr now points to element #2)
Pointer Arithmetic
• Another example:
double sales[5];
double *salesPtr = NULL;
salesPtr = sales (Assume again that array begins at 100)
salesPtr++; (Sets salesPtr to 108)
salesPtr+=2; (Sets salesPtr to 124)
Pointers and Arrays
Pointers and Arrays
• Now that we understand pointer arithmetic, we
can finally see that pointers and arrays are really
related.
• In face, we will now see that they can in fact be
used interchangeably.
• Suppose we have the following:
int x[5] = {45, 55, 65, 75, 85};
int *xPtr = NULL;
• Then, suppose that we want to print all the
elements in the array.
– We now have two options to get the job done.
Pointers and Arrays
• Option 1: Array Notation
– We already know how to do this: just create a for loop and
print each element one at a time.
– For example:

int i;
int x[5] = {45, 55, 65, 75, 85};
for (i=0; i<5; i++) {
printf (“%d\t”, x[i]);
}
Pointers and Arrays
• Option 2: Pointer / Offset Notation
– in this option, we use our knowledge of pointer arithmetic to
achieve the same goal.
int offset = 0;
int x[5] = {45, 55, 65, 75, 85};
int *xPtr = NULL;
/* point to zeroeth element to start */
xPtr = &x;
/* use pointer / offset noation */
for (offset = 0; offset < 5; offset++) {
printf (“%d\t”, *(xPtr + offset));
}
Example: Pointers and Arrays
Example
• Let’s examine a concrete example.
• The goal of the program is to copy the contents of
one array to another array.
• The program has two functions:
– copy1: uses array notation
– copy2: uses pointer / offset notation
– they both achieve the same goal.
#include <stdio.h>

void copy1 (char [], char []);


void copy2 (char *, char*);
#define MAXCHARS 10

main () {
char string1[MAXCHARS], string3[MAXCHARS];
char string2 [MAXCHARS] = "Hello";
char string4 [MAXCHARS] = "Good Bye";

copy1 (string1, string2);


printf ("%s %s\n", string1, string2);
copy2 (string3, string4);
printf ("%s %s", string3, string4);
getchar();
}
void copy1 (char s1[], char s2[]) {
int i;
for (i=0; i<MAXCHARS; i++)
s1[i]= s2[i];
}

void copy2 (char *s1, char *s2) {


int offset;
for (offset=0; offset < MAXCHARS; offset++)
*(s1+offset) = *(s2+offset);
}

Hello Hello
Good Bye Good Bye

You might also like