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.

ExpressionAddress calculationResult
int *ip = (int *)0x10; ip + 20x10 + 2 × 40x18
char *cp = (char *)0x10; cp + 40x10 + 4 × 10x14
char **pp = (char **)0x10; pp + 20x10 + 2 × 80x20

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

TaskC formMeaning
Declare a pointerint *xp;xp can store an int address.
Store an addressxp = &x;Make xp point to x.
Read through pointery = *xp;Copy the pointed-to value into y.
Write through pointer*xp = y;Store y where xp points.
Array value accessA[n] or *(A + n)Read or write entry n.
Array address access&A[n] or A + nAddress of entry n.

Check Yourself

Try these before opening the answers. The values differ from Worksheet 07.

  1. Given int count = 7;, write one declaration and assignment that makes an int * named cp point to count.
  2. If int x = 3; int *xp = &x; *xp = 8;, what is the value of x? Why?
  3. On this platform, what address is produced by adding 3 to an int * whose current address is 0x40?
  4. Rewrite values[4] using pointer notation.
  5. What is the difference between p + 2 and *(p + 2)?

Answers

Show the answers
  1. int *cp = &count;. The expression &count is the address that an int * needs.
  2. x is 8. xp stores the address of x, so assigning to *xp writes directly to x.
  3. 0x4C. The calculation is 0x40 + 3 × 4, because an int uses 4 bytes.
  4. *(values + 4).
  5. p + 2 is 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.