CSSE 132 Mini-reading
Pointers and Arrays in 10 Minutes
Review addresses, pointer syntax, dereferencing, pointer arithmetic, and the connection between arrays and pointers in C.
Why This Matters
Pointers give C code a way to name a location in memory and then read or change the value stored there. In CSSE 132, you will use them to trace C code, connect C to memory diagrams and assembly, and access array elements. These are the ideas practiced in Worksheet 07.
Core Idea
Every variable has both a value and a memory
address. A pointer is a variable whose value is an address.
For example, if x is an int, then an
int * can hold the address of x.
& asks “where?”
&x is the address of x. Use it to put an
address into a compatible pointer.
* asks “what is there?”
*xp is the value stored at the address in xp.
It can be read or assigned.
The type matters. int *xp means that xp holds
the address of an int, so dereferencing xp produces
an int. The star has two related jobs: in a declaration it makes
a pointer type; elsewhere it dereferences a pointer.
Worked Examples
Example 1: Declare, initialize, and dereference
int x = 12;
int *xp = &x;
int y = *xp;
*xp = 5;
After the second line, xp stores the address of
x. Therefore *xp refers to the same storage as
x. The third line copies 12 into y. The final line
changes the value stored at that address, so x becomes 5 while
y remains 12.
Example 2: Trace a pointer assignment
int x = 2;
int y = 0;
int z;
int *xp = &x;
*xp = y;
z = *xp;
xp points to x. Thus *xp = y writes
0 into x, and z = *xp then reads that same 0.
The final values are x = 0, y = 0, and
z = 0. Draw the arrow from xp to x:
the arrow identifies a location, not a separate copy of x.
Example 3: Pointer arithmetic moves by objects
If p has type T *, then
p + n has address:
address(p + n) = address(p) + n × sizeof(T)
It points to the nth T object after the one
pointed to by p. On the 64-bit CSSE 132 platform, an
int is 4 bytes, a char is 1 byte, and a pointer is
8 bytes.
| Expression | Address calculation | Result |
|---|---|---|
int *ip = (int *)0x10; ip + 2 | 0x10 + 2 × 4 | 0x18 |
char *cp = (char *)0x10; cp + 4 | 0x10 + 4 × 1 | 0x14 |
char **pp = (char **)0x10; pp + 2 | 0x10 + 2 × 8 | 0x20 |
Do pointer arithmetic only within an actual array (or one position past its end). The made-up addresses above are for practicing the calculation, not addresses a program should dereference.
Example 4: Arrays and pointers
int scores[3] = {4, 9, 2};
int *p = scores;
printf("%d\n", scores[1]);
printf("%d\n", *(p + 1));
printf("%p\n", (void *)&scores[2]);
printf("%p\n", (void *)(scores + 2));
The array name scores represents the address of its first
entry in these expressions. Both value-printing lines print 9 because
scores[1] means *(scores + 1). Likewise,
&scores[2] and scores + 2 name the address of the
third entry. Arrays are a continuous sequence of elements, which is why adding
1 moves to the next element of the array.
Common Mistakes
Mixing up & and *
&x gives the address of x. If xp
already stores that address, *xp gives the value at it.
Forgetting that the type scales addition
ip + 1 for an int * moves 4 bytes here; it does
not simply add 1 to the numeric address.
Changing the pointer instead of the pointed-to value
p + 3 computes an address. To access the value there, write
*(p + 3). To change it, assign through that expression.
Using an invalid address
Dereferencing an uninitialized, null, or out-of-range pointer is not a way to get a default value. It is an error and can crash the program.
Quick Reference
| Task | C form | Meaning |
|---|---|---|
| Declare a pointer | int *xp; | xp can store an int address. |
| Store an address | xp = &x; | Make xp point to x. |
| Read through pointer | y = *xp; | Copy the pointed-to value into y. |
| Write through pointer | *xp = y; | Store y where xp points. |
| Array value access | A[n] or *(A + n) | Read or write entry n. |
| Array address access | &A[n] or A + n | Address of entry n. |
Check Yourself
Try these before opening the answers. The values differ from Worksheet 07.
- Given
int count = 7;, write one declaration and assignment that makes anint *namedcppoint tocount. - If
int x = 3; int *xp = &x; *xp = 8;, what is the value ofx? Why? - On this platform, what address is produced by adding 3 to an
int *whose current address is0x40? - Rewrite
values[4]using pointer notation. - What is the difference between
p + 2and*(p + 2)?
Answers
Show the answers
int *cp = &count;. The expression&countis the address that anint *needs.xis 8.xpstores the address ofx, so assigning to*xpwrites directly tox.0x4C. The calculation is0x40 + 3 × 4, because anintuses 4 bytes.*(values + 4).p + 2is the address two elements later;*(p + 2)is the value stored at that address.
Next Step
Complete Worksheet 07, then practice drawing an arrow from each pointer to the variable or array element whose address it stores before tracing the code.