Concepts / Pointers and Addresses

Pointers and Addresses

In a declaration, [ ] means array of and * means pointer to.

  • Programming

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
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.

Notes on the code
  • char: the type of each element
  • v: the name of the whole collection
  • [6]: the array has six elements
charelement typevname of the collection[6]six elementsthenthen
In the declaration char v[6], which part gives the element type, which part names the collection, and which part gives the count?

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.

Worked example

Counting the elements in a new array

How many elements does the declaration char w[10]; create, and what is the last valid index?

  1. 1
    Identify the element type

    The word char says each element is a char.

  2. 2
    Read the count

    The number inside the brackets is 10, so the declaration creates ten elements.

  3. 3
    Apply the lower bound

    The first index is 0, so the indices run from 0 up to 9.

Result

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.

v[0]first valid indexv[1]validv[2]validv[3]validv[4]validv[5]last valid indexv[6]outside v
If an array has six elements, which slots exist, where does index 0 sit, and why is the last valid index 5?

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.

Notes on the code
  • 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.

ppointer variable holding anaddresschar objectone object of type charrefers to
What does a pointer variable store, and how does it point to a separate value?

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.

FeatureArray declaration: char v[6];Pointer declaration: char *p;
Marker symbolBrackets [ ], read as array ofStar *, read as pointer to
What the name refers toA collection of six char elementsA variable that holds an address
What it describesA block of storage with several slotsA reference to a location
Valid index or valueIndices 0 to 5An 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 like

    Reading char v[6]; and expecting v[1] to be the first element.

    Why it fails

    Every array has 0 as its lower bound, so the first valid index is 0.

    Do this instead

    Use 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 like

    Writing v[6] for an array of six elements.

    Why it fails

    Valid indices for an array of size n run from 0 to n-1, so for six elements the last is 5.

    Do this instead

    Compute 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 like

    Reading char *p; as if it creates a block of char elements.

    Why it fails

    The star declares a pointer variable meant to hold the address of one object of type char.

    Do this instead

    Read 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 like

    Assuming char *p can hold the address of any kind of object.

    Why it fails

    A pointer declared as char * is meant for objects of type char, and not for objects of some other type.

    Do this instead

    Match the pointer's declared type to the kind of object whose address it will hold.

Checking Your Reading

EASY

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.

Related concepts

More concepts starting with P