Pointers and Arrays in Computer Science II - Lecture 5, Study notes of Data Structures and Algorithms

A part of the lecture notes for computer science ii, specifically for lecture 5. It covers the topics of pointers and arrays, including how pointers store memory addresses, pointer arithmetic, and array initialization. The document also includes examples and exercises to help students understand these concepts.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-v4p
koofers-user-v4p 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Lecture 5
Pointers, Arrays, Pointer Arithmetic
Review from Lecture 4
Extended example of student grading program
Passing comparison functions to sort
Non-member operators
Today’s Lecture Pointers and Arrays
Pointers
Arrays, array initialization and string literals
Arrays and pointers
Reading: Ford & Topp, pp. 219-228
Overview
Pointers store memory addresses.
They can be used to access the values stored at their stored memory address.
They can be incremented, decremented, added and subtracted.
Dynamic memory is accessed through pointers.
Pointers are also the primitive mechanism underlying vector iterators, which we have
used with std::sort and will continue to use many times throughout the semester.
Pointer Example
Consider the following code segment:
float x = 15.5;
float *p;
p = &x;
*p = 72;
if(x>20)
cout << "Bigger\n";
else
cout << "Smaller\n";
The output is
Bigger
because x == 72.0. What’s going on?
pf3
pf4
pf5

Partial preview of the text

Download Pointers and Arrays in Computer Science II - Lecture 5 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Lecture 5

Pointers, Arrays, Pointer Arithmetic

Review from Lecture 4

  • Extended example of student grading program
  • Passing comparison functions to sort
  • Non-member operators

Today’s Lecture — Pointers and Arrays

  • Pointers
  • Arrays, array initialization and string literals
  • Arrays and pointers

Reading: Ford & Topp, pp. 219-

Overview

  • Pointers store memory addresses.
  • They can be used to access the values stored at their stored memory address.
  • They can be incremented, decremented, added and subtracted.
  • Dynamic memory is accessed through pointers.
  • Pointers are also the primitive mechanism underlying vector iterators, which we have used with std::sort and will continue to use many times throughout the semester.

Pointer Example

Consider the following code segment:

float x = 15.5; float *p; p = &x; *p = 72; if ( x > 20 ) cout << "Bigger\n"; else cout << "Smaller\n";

The output is

Bigger

because x == 72.0. What’s going on?

Pointer Variables and Memory Access

  • x is an ordinary integer, but p is a pointer that can hold the memory address of an integer variable.
  • The difference is explained in the following picture:
  • Every variable is attached to a location in memory where the value of the variable is stored. Hence, we draw a picture with the variable name next to a box that represents the memory location.
  • Each memory location also has an address, which is itself just an index into the giant array that is the computer memory.
  • The value stored in a pointer variable is an address in memory. In this case, the statement

p = &x;

Takes the address of x’s memory location and stores it (the address) in the memory location associated with p.

  • Since the value of this address is much less important than the fact that the address is x’s memory location, we depict the address with an arrow rather than with its actual value. - We say that p “points to” x.
  • The statement

*p = 72;

causes the computer to get the memory location stored at p, then go to that memory location, and store 72 there. This writes the 72 in x’s location.

  • *p is an l-value here.

Exercise

What is the output of the following code sequence?

int x = 10, y = 15; int *a = &x; cout << x << " " << y << endl; int *b = &y; *a = x * *b; cout << x << " " << y << endl; int *c = b; *c = 25; cout << x << " " << y << endl;

Null Pointers

  • Pointers that don’t (yet) point anywhere useful should be given the value 0, a legal pointer value. - Most compilers define NULL to be a special pointer equal to 0.
  • Comparing a pointer to 0 is very useful. It can be used to indicate whether or not a pointer has a legal address. (But don’t make the mistake of assuming pointers are automatically initialized to 0.) For example,

if ( p != 0 ) cout << *p << ’\n’

tests to see if p is pointing somewhere that appears to be useful before accessing the value stored at that location.

  • Dereferencing a null pointer leads to memory exceptions (program crashes).

Arrays

  • Here’s a quick example to remind you about how to use an array:

const int n = 10; double a[n]; int i; for ( i=0; i<n; ++i ) a[i] = sqrt( double(i) );

  • Remember: the size of array a is fixed at compile time. vectors act like arrays, but they can be grown and shrunk dynamically in response to the demands of an application.

Stepping through Arrays with Pointers

  • Pointers are the iterators for arrays.
  • The array initialization code above,

const int n = 10; double a[n]; int i; for ( i=0; i<n; ++i ) a[i] = sqrt( double(i) );

can be rewritten as

const int n = 10; double a[n]; double *p; for ( p=a; p<a+n; ++p ) *p = sqrt( p-a );

  • The assignment

p = a;

takes the address of the start of the array and assigns it to p.

  • This illustrates the important fact that the name of an array is in fact a pointer to the start of a block of memory. - We will come back to this several times!

The line p=a could also have been written:

p = &a[0];

which means “find the location of a[0] and take its address”.

  • The test p<a+n checks to see if the value of the pointer (the address) is less than n array locations beyond the start of the array. - We could also have used the test p != a+n
  • By incrementing, ++p, we make p point to the next location in the array.
  • In the assignment

*p = sqrt( p-a )

p-a is the number of array locations between p and the start. This is an integer. The square root of this value is assigned to *p.

  • We will draw a picture of this in class.