YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Memory & dataChapter 25 of 73

Arrays and pointers together

Explain array-to-pointer conversion, adjusted parameters, pointer-to-array declarations, and two-dimensional rows.

5 min readFoundationsC17 baseline
Offline reading
In this chapter9 sections
0% through this chapter

Arrays and pointers often appear together, but they play different roles. An array contains elements; a pointer value can refer to an element. We will track the conversion between them and see why size and row shape still matter.

1. A conversion is not an identity

In most expression contexts, an array expression is converted to a pointer to its first element. This is often called array decay. The original array object does not turn into a pointer object. The converted value provides a way to access its elements; it does not carry a built-in count of all available elements.

C17 · illustrative fragment
int values[3] = {4, 5, 6};
int *first = values;
size_t count = sizeof values / sizeof values[0];

Here values is an array of three integers and first is a separate pointer object. The declaration initializes the pointer using a converted array expression. sizeof values obtains the array’s complete byte size, while sizeof first would obtain the pointer object’s size. Do not teach that these sizes must be unequal: a particular implementation can happen to make them equal.

2. Context can preserve the array type

Important C17 cases without the ordinary conversion include an array operand of sizeof, unary & applied to the array, and a string literal used to initialize an array. Thus &values has pointer-to-array type, while the usual conversion of values has pointer-to-first-element type. Similar displayed addresses do not make those pointer types interchangeable.

C17 · illustrative fragment
int values[3] = {4, 5, 6};
int (*whole)[3] = &values;

The parentheses matter: int (*whole)[3] declares a pointer to an array of three integers. Removing the parentheses in int *whole[3] would instead declare an array of three pointers. Read the declarator’s structure rather than counting star symbols.

3. An array parameter is adjusted to a pointer

Complete C17 program
#include <assert.h>
#include <stddef.h>

static int total(const int values[], size_t count)
{
    int result = 0;
    for (size_t i = 0; i < count; ++i) {
        result += values[i];
    }
    return result;
}

int main(void)
{
    int values[3] = {4, 5, 6};
    assert(total(values, 3) == 15);
    return 0;
}

In this parameter declaration, const int values[] is adjusted to const int *values. No array copy is made for the parameter, and sizeof values inside the function would describe the pointer parameter, not the caller’s array. Writing [3] in an ordinary array parameter would not by itself enforce a three-element argument.

The separate count is part of the interface contract. For this example the caller supplies at least that many readable integers, and every running partial sum in this loop must fit in int. A pointer plus a count is useful only when they agree. The special parameter syntax values[static 3] expresses a minimum access requirement under C’s rules; it is not an automatic runtime bounds checker.

4. A two-dimensional array points to rows

C17 · illustrative fragment
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*row)[3] = grid;

The first element of grid is a row of three integers. Its ordinary conversion therefore yields int (*)[3], not int **. Advancing row by one advances by one whole row. A separately created array of pointers can represent another data arrangement; it is not interchangeable with this contiguous two-dimensional array.

5. Choose the question before choosing the syntax

Use an array when the object itself should contain a sequence of elements. Use a pointer when a value should designate an existing object, an element, or a permitted boundary. Use a pointer-to-array when the target’s row shape matters to the type. In an interface, document the count, mutability, and lifetime instead of hoping pointer syntax communicates all three.

Primary references and further reading

Optional visual model: Move through an array

Move through an array

VISUAL MODEL

Choose an index. See which element the pointer reaches, including the one-past boundary.

Inside a function, a contains three initialized int elements. Index 3 is a boundary, not a fourth element.

  1. int a[3] = {3, 6, 9};
  2. int *p = a;
  3. p = a + 1;
  4. if (p != a + 3) { int value = *p; (void)value; }
inta[0]3Array element
inta[1]6Array element
inta[2]9Array element
a + 3a + 3—One-past; do not read
p refers to
—
Value read
—

a has three elements: 3, 6, and 9. The pointer initializer has not been reached.

Pointer steps count elements, not a fixed number of bytes. A one-past pointer may be formed and compared here, but not dereferenced. No out-of-bounds read is executed.

Read the complete trace
  1. a has three elements: 3, 6, and 9. The pointer initializer has not been reached.
  2. In this initializer, a converts to a pointer to its first element. p points to a[0].
  3. p receives a + 1. Adding an index moves by that many int elements.
  4. This pointer refers to an element. Reading *p yields 6. a[1] names the same element.

Before you move on

  • An array object and a pointer object have different types and roles.
  • Many array expressions convert to a pointer to their first element.
  • An adjusted array parameter is a pointer, so its sizeof does not recover the caller’s array size.
Keep these ideas separate
Pointer to element / Pointer to row
A two-dimensional array commonly converts to a pointer to its row type, rather than int **.
Explain it in your own words

Why should a function receiving an array view also receive an appropriate element count?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

For int a[3], what is permitted for the pointer a + 3?
Read the explanation for every choice
  1. Form it as one-past, but do not dereference it.

    ✓ Correct reasoning. The one-past value is a permitted boundary; it does not designate an array element to read.

  2. Read *(a + 3) because the pointer can be formed.

    Being allowed to form a pointer does not make every dereference valid.

  3. It necessarily points to another object.

    C provides no guarantee of another usable object at that boundary.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 25 / 73

In this chapter

Arrays and pointers together

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping