Pointers Preparation
Overview
We’ve been avoiding talking much about those weird *
that show up in various places in C, but now it’s time to dive in to one of the most powerful but feared features of C: pointers! This is one of the aspects of C that makes it a “lower level” language, since pointers give you much more control and access, but come with additional complexity and responsibility.
Basic Learning Objectives
Before class, you should be able to:
- Explain the difference between
int
andint *
- Explain the general idea of pointers
- Identify some of the benefits of using pointers
Advanced Learning Objectives
After class, you should be able to:
- Correctly use
*
and&
operators - Explain the benefits and reasons to use pointers
Resources
You should read/watch the following (if you aren’t a fan of goofy videos, you can skip the first one):
- Binky Pointer Video - silly intro to pointers
- DIS 2.1 Parts of Program Memory and Scope
- DIS 2.2 C Pointer Variables
- DIS 2.3 Pointers and Functions
- Pointer basics from Standford
Checks
Submit answers to the following on Moodle:
-
Given a variable
int count=5
, what is the C code to define a variablecount_ptr
that holds the memory address ofcount
? -
Complete the following code that swaps the values stored at two
char
pointers:
There are a lot more exercises on DIS if you’d like more practice: