Previous Lecture Lecture 8 Next Lecture

Lecture 8, Thu 07/26

Introduction to Pointers

Memory Addresses

int i = 1;
cout << &i << endl;

Pointers

Example of declaring a pointer

int* p; // p is a pointer to an int type
cout << sizeof(p) << endl; // pointer address is 8 bytes since I have a 64-bit OS
cout << p << endl;
int i = 1;
//int* p = i; 		// can’t do this! i is an int, not a pointer to an int
int* p = &i; 		// OK. p points to the address of i
cout << p << endl;
int* q = p; 		// make q point to the same place p does (& not needed)
cout << q << endl;

Dereferencing Pointers

Larger example with dereferencing pointers

int i = 1;
int* p = &i; // point to memory location of int i var

cout << "p points to the memory address " << p << endl;
cout << "p points to the integer value " << *p << endl;
cout << "----" << endl;

*p = 10; // change value in the memory address.

cout << "p points to memory address " << p << endl;
cout << "p points to the integer " << *p << endl;
cout << "i = " << i << endl;
cout << "----" << endl;

i = 25;

cout << "p points to memory address " << p << endl;
cout << "p points to the integer " << *p << endl;
cout << "i = " << i << endl;

p = NULL; // or assign p = 0;
cout << p << endl;
cout << *p << endl;

Example of pointers to pointers

// A larger example (with pointers to pointers)
int x = 5, y = 10, z = 15;
int* p1 = &x;
int* p2 = &y;

p2 = p1;
int** p3; // pointer pointing to another pointer
p3 = &p1;
p1 = &z;

x /= 5;
y /= 5;
z /= 5;

cout << "x = " << x << endl;
cout << "&x = " << &x << endl;
cout << "y = " << y << endl;
cout << "&y = " << &y << endl;
cout << "z = " << z << endl;
cout << "&z = " << &z << endl;
cout << "----" << endl;

cout << "&p1 = " << &p1 << endl;
cout << "p1 = " << p1 << endl;
cout << "*p1 = " << *p1 << endl;
cout << "----" << endl;

cout << "&p2 = " << &p2 << endl;
cout << "p2 = " << p2 << endl;
cout << "*p2 = " << *p2 << endl;
cout << "----" << endl;

cout << "&p3 = " << &p3 << endl;
cout << "p3 = " << p3 << endl;
cout << "*p3 = " << *p3 << endl;
cout << "**p3 = " << **p3 << endl;

Code Trace

Pass-by-reference vs. pass-by-value

// pass by value
void addTwo(int x) {
	x = x + 2;
	cout << x << endl;
}

int main() {
	int a = 10;
	cout << "a = " << a << endl; // 10
	addTwo(a);
	cout << "a = " << a << endl; // 10
}
// pass by reference
void addTwo(int &x) {
	x = x + 2;
	cout << x << endl;
}

int main() {
	int a = 10;
	cout << "a = " << a << endl; // 10
	addTwo(a);
	cout << "a = " << a << endl; // 12 – changes value in memory location
}
void addTwo(int* x) {
	*x = *x + 2;
	cout << *x << endl;
}

int main() {
	int a = 10;
	cout << "a = " << a << endl;
	addTwo(&a); // pointers are memory addresses, need to pass this.
	cout << "a = " << a << endl;
}

Passing arrays into functions

void printArray(int values[], int size) {
	// Note: size is passed in since we won’t know how to get the size with
	// just the array variable.

	cout << values << endl; // prints memory location of start of values[]

	for (int i = 0; i < size; i++) {
		cout << "values[" << i << "] = " << values[i] << endl;
		cout << "&values[" << i << "] = " << &values[i] << endl;
		// Note: The address values are 4 bytes away since an int type is 4 bytes
	}
}

int main() {
	int values[3] = { 5, 10, 15 };
	printArray(values, 3);
	return 0;
}
void printArray(int* values, int size)