Pointers and Addresses
In a declaration, [ ] means array of and * means pointer to.
Two Declarations That Look Alike
A declaration is a short line of code that tells the language what a name refers to and what kind of data it holds. Two of the most common declarations in C look similar on the page but do very different jobs. One creates a collection of values. The other creates a variable that refers to a single object somewhere else. Before reading the rules, try to predict what each line is for.
What do you think happens?
The declaration char v[6]; is written. How many values does it create, and how are they reached?
Reveal answer
It creates an array of six char elements, indexed from v[0] to v[5].
The brackets tell you the name v names a collection, and the number inside the brackets gives the count of elements.
Reading an Array Declaration
In a declaration, the brackets [ ] mean array of. The declaration char v[6]; creates an array of six char elements. Read it from left to right: the type char says what each element holds, the name v is the collection, and the number inside the brackets says how many elements exist.
- char: the type of each element
- v: the name of the whole collection
- [6]: the array has six elements
The declaration fixes the number of elements at six. The source also notes that the size given in an array declaration must be a constant expression, so the count is known when the declaration is read rather than decided later.
Counting the elements in a new array
How many elements does the declaration char w[10]; create, and what is the last valid index?
- 1Identify the element type
The word char says each element is a char.
- 2Read the count
The number inside the brackets is 10, so the declaration creates ten elements.
- 3Apply the lower bound
The first index is 0, so the indices run from 0 up to 9.
The declaration creates ten char elements, indexed w[0] through w[9]. The last valid index is 9.
Why Indexing Starts at Zero
Every array starts at index 0, which is its lower bound. The lower bound is the first valid index, and it is not 1. Because of this, an array declared with six elements has indices running from 0 up to 5. The index 6 does not belong to v at all.
The rule to remember is simple. For an array of size $n$, the valid indices run from $0$ to $n-1$. For v, with $n = 6$, the valid range is 0 to 5. A reference to v[6] falls outside that range.
Reading a Pointer Declaration
A pointer is declared in a similar form, but the star symbol does the work that the brackets did for the array. The source reads the star as pointer to. The declaration char *p; creates a pointer variable meant to hold the address of an object of type char.
- char: the type of object the pointer is meant to refer to
- *: marks p as a pointer
- p: the name of the pointer variable
What a pointer variable holds is an address. The source says that a pointer variable can hold the address of an object of the appropriate type. The type in the declaration tells you what kind of object that address is supposed to point to. A pointer declared as char * is meant for objects of type char, and not for objects of some other type.
Array Versus Pointer Declarations
The two declarations use nearly the same pattern, and that is why they are easy to confuse. The difference lies in what each declaration says about the name. The array declaration says the name is a collection of six values. The pointer declaration says the name holds the location of one object of the given type.
| Feature | Array declaration: char v[6]; | Pointer declaration: char *p; |
|---|---|---|
| Marker symbol | Brackets [ ], read as array of | Star *, read as pointer to |
| What the name refers to | A collection of six char elements | A variable that holds an address |
| What it describes | A block of storage with several slots | A reference to a location |
| Valid index or value | Indices 0 to 5 | An address of a char object |
Comparing how the two declarations are written and what they mean.
Mistakes Beginners Make with Indices and Pointer Types
Common mistake Assuming the first index of an array is 1
What it looks likeReading char v[6]; and expecting v[1] to be the first element.
Why it failsEvery array has 0 as its lower bound, so the first valid index is 0.
Do this insteadUse v[0] for the first element and v[5] for the last of the six.
Common mistake Using the array size as the last index
What it looks likeWriting v[6] for an array of six elements.
Why it failsValid indices for an array of size n run from 0 to n-1, so for six elements the last is 5.
Do this insteadCompute the last valid index as n-1 before writing the reference.
Common mistake Treating a pointer declaration as if it creates several values
What it looks likeReading char *p; as if it creates a block of char elements.
Why it failsThe star declares a pointer variable meant to hold the address of one object of type char.
Do this insteadRead the star as pointer to, and understand that the name holds a location, not a collection.
Common mistake Ignoring the type in a pointer declaration
What it looks likeAssuming char *p can hold the address of any kind of object.
Why it failsA pointer declared as char * is meant for objects of type char, and not for objects of some other type.
Do this insteadMatch the pointer's declared type to the kind of object whose address it will hold.
Checking Your Reading
The declaration char w[10]; is written. State how many elements it creates, give the first and last valid indices, and say whether the pointer declaration char *q; would create a block of ten elements.
Hints
- The number inside the brackets gives the element count.
- The valid indices for an array of size n run from 0 to n-1.
- The star declares a pointer variable, which holds the address of one object.
Key Takeaways
- In a declaration, [ ] means array of and * means pointer to.
- The declaration char v[6]; creates an array of six char elements, indexed from v[0] to v[5].
- Every array has 0 as its lower bound, so the valid indices for an array of size n run from 0 to n-1.
- The declaration char *p; creates a pointer variable meant to hold the address of an object of type char.
- An array declaration describes a collection of values, while a pointer declaration describes a variable that refers to a single object elsewhere.