Pointers: addresses with a type
Read addresses, dereference safely, and understand the close—but not identical—relationship between arrays and pointers.
After this lesson
You should be able to
- Use address-of and dereference operators with initialized pointers.
- Explain array-to-pointer conversion and pointer arithmetic within one array.
The address is not the value
A pointer stores the address of an object and carries the pointed-to type. int *p = &score; makes p point to score. *p reads or writes the score object. Dereferencing an uninitialized, null, or no-longer-valid pointer is undefined behavior.
Draw two boxes: one for score and one for p. Put score's value in the first and score's address in the second. This picture prevents the common mistake of treating p and *p as interchangeable.
int score = 72;
int *p = &score;
*p = 80;
printf("%d\n", score); /* 80 */Arrays often become pointers
In most expressions, an array name converts to a pointer to its first element. Therefore a[i] is defined in terms of *(a + i). Pointer arithmetic advances by elements, not raw bytes.
An array is still not a pointer object: it owns fixed storage and cannot be assigned to a different address. Pointer arithmetic is defined only within the same array object or one position past it; the one-past pointer must not be dereferenced.
Try it yourself
Using a pointer, double every element in an integer array.
Need a hint?
Begin at the first element and stop before values + count.
Check the worked solution
Advance one element per iteration and multiply the dereferenced value.
for (int *p = values; p < values + count; p++) {
*p *= 2;
}Quick check
If p points to an int, what does p + 1 point to?
Why this lesson exists
Syllabus mapping
Pointers · Pointers to arrays
Maps to course outcomes CO3, CO5.