Deadlines/Announcements

😫 Project 4 due Sunday, Feb 20th

this week’s slides: bit.ly/cs31w22_week7

last week’s slides: bit.ly/cs31w22_week6

credit: nikki woo+zhehui+rosemary

Today's Agenda

Pointers

What exactly is a pointer?

A variable that holds the ADDRESS of another variable.

// declaring a pointer
*data_type** pointer;

// where *data_type* is a ... data type
// note that each pointer must specify what data type the object is
// that we're storing the address for 
// (and you can't convert/assign pointers of type A to pointers of type B)

How do we get the Address of Another Object?


// use & (ampersand)
#include <string>
#include <iostream>
using namespace std;
int i1 = 0; // data type = int
int* finger = &i1; // create a pointer to an int datatype, 
									  // point to i1's address that we get using &

// i1             is an **int**
// int* finger    creates a integer pointer named finger
// &i1            the memory address of i1
// finger = &i1.  sets finger to point at the address of i1
// finger         is now an **int pointer** to i1's address

[ memory: value ] ← pointer

*pointer = memory: value

pointer [ ]

What can we do to pointers?

// we can reassign them
int i2 = 10; 
int *other_pointer = &i2;
finger = other_pointer;

// value of finger is now 10