Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 135 ยท Pointers

Pointers and Arrays

Understand how C arrays turn into pointers, why a[i] means *(a + i), how to pass arrays to functions safely, and why sizeof gives different answers.

In C, arrays and pointers are closely related, but they're not the same thing. An array is a block of elements stored side by side. A pointer is a variable that holds an address. The connection between them is one rule: in almost every expression, an array is automatically converted into a pointer to its first element. This conversion is called array decay, and it explains a surprising amount of C: why a[i] works on pointers, why functions can't receive a whole array, why sizeof suddenly gives the "wrong" answer inside a function, and why you can't assign one array to another.

This lesson takes that rule apart piece by piece. You'll see the exact places where arrays do not decay, how array parameters really work, the difference between a and &a, and how pointers to arrays let you pass two-dimensional data to functions. It builds directly on Pointer Arithmetic and Arrays.

Array decay

When you use an array's name in an expression, C gives you &array[0], a pointer to the first element. The type changes too: an int[5] becomes an int *. That's why you can assign an array to a pointer without &:

C An array turns into a pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

All four expressions read the same element, because the subscript operator is defined in terms of pointer arithmetic: a[i] means exactly *(a + i). With an array, a first decays to a pointer, then the arithmetic and dereference happen. With a pointer, there's nothing to decay. Either way, [] is just a shorter way to write "move i elements and dereference".

A famous consequence: addition doesn't care about order, so *(a + i) equals *(i + a), which means 3[numbers] is legal C and equals numbers[3]. Never write it in real code, but it shows that [] really is pointer arithmetic in disguise.

Where arrays don't decay

The decay rule has three exceptions. In these places the array stays an array:

  1. As the operand of sizeof: sizeof numbers is the size of the whole array.
  2. As the operand of &: &numbers is a pointer to the whole array (more on that below).
  3. A string literal used to initialize a char array: char word[] = "hi"; copies the characters into the array.

C23 adds a fourth one you'll rarely meet: the operand of typeof. Everywhere else, including when you pass an array to a function, return it, compare it or add to it, the array becomes a pointer.

sizeof: arrays versus pointers

Because sizeof is one of the exceptions, it measures an array and a pointer very differently. On our 64-bit server, a pointer is 8 bytes no matter how big the thing it points to is:

C sizeof an array and a pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

sizeof readings is 80: ten doubles of 8 bytes. sizeof p is 8, the size of the pointer itself. The expression sizeof readings / sizeof readings[0] is the standard way to compute an array's length, and it only works on a real array. Applied to a pointer, it would compute 8 / 8 and give 1, a silent bug.

The same goes for strings: name is an array of 4 chars, while label is a pointer to a string literal stored elsewhere. You'll see more about that difference at the end of this lesson.

Arrays as function parameters

You can't pass an array to a function by value. When you call f(numbers), the array decays, and the function receives a pointer to the first element. C even rewrites the parameter declaration to match: a parameter written as int values[] or int values[100] is an int *values. The number in the brackets doesn't change the type. (Recent GCC versions do use it as a hint and warn when you pass a smaller array, a handy bonus.)

That has two big consequences:

  • The function works on the caller's array, not a copy. Changes are visible to the caller.
  • The function has no idea how long the array is. You must pass the length separately.
C Array parameters are pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

This example produces a compiler warning on purpose. GCC notices sizeof values inside report_size and warns that sizeof on an array function parameter will return the size of int * (the -Wsizeof-array-argument warning, on by default). The output confirms it: 16 in main, 8 inside the function. That's the bug the warning protects you from: computing an element count inside the function would give 2 instead of 4.

The last call shows a nice benefit of decay: since the function only gets a pointer and a count, data + 2 with a count of 2 passes "the array starting at index 2". You can hand any slice of an array to a function this way.

Tip: Use const on array parameters the function only reads, as print_all does with const int values[]. It documents the intent, lets callers pass read-only data, and the compiler stops you from writing to it by accident. const and Pointers explains the rules.

Bracket or star?

Since int values[] and int *values mean the same thing in a parameter list, which should you write? Both are common. Many programmers use [] to signal "this expects an array of several elements" and * for "a pointer to one object". Some style guides insist on * everywhere because it's honest about the type. Pick one convention per project.

C99 added a lesser-known form that actually carries information: void process(int values[static 4]) promises the caller passes at least 4 valid elements. Compilers can use it to warn when you pass a smaller array or NULL, but they're not required to check. You'll see it occasionally in modern code.

Functions can't return arrays

A function's return type can't be an array, and returning the address of a local array gives the caller a dangling pointer, because the local array disappears when the function returns. The usual solutions are to let the caller provide the buffer (as fgets and snprintf do), to return a struct that contains an array, or to allocate memory dynamically. Pointers and Functions shows each one.

a versus &a

numbers (decayed) and &numbers hold the same address, but they have different types, and pointer arithmetic proves it. numbers decays to int *, a pointer to one element. &numbers is int (*)[5], a pointer to the whole five-element array. Adding 1 to each moves by very different amounts:

C Pointer to an element vs pointer to the array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

first + 1 moves 4 bytes (one int), while whole + 1 moves 20 bytes (a whole array of five ints). Dereferencing whole gives back the array itself, so (*whole)[2] is numbers[2], and sizeof *whole is 20.

The declaration int (*whole)[5] needs those parentheses. Without them, int *whole[5] declares an array of five pointers, a completely different thing. Read declarations from the name outward: (*whole) "whole is a pointer", [5] "to an array of 5", int "ints".

Two-dimensional arrays and pointers to arrays

Pointers to arrays look like a curiosity, but you meet them every time you pass a 2D array to a function. A 2D array is an array of arrays: int grid[3][4] is an array of 3 elements, each of which is an int[4]. When grid decays, it becomes a pointer to its first element, which is a pointer to an array of 4 ints: int (*)[4]. It is not an int **.

C Passing a 2D array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The parameter int grid[][COLS] is rewritten to int (*grid)[COLS]. The compiler needs the column count, because to find grid[r][c] it must skip r whole rows of COLS elements. Only the first dimension can be left empty. Each row++ jumps a full row of 16 bytes.

A common mistake is declaring the parameter as int **grid. That type describes a pointer to an array of pointers, a different memory layout, and passing a real 2D array to it is an error in GCC 14 and later ("passing argument 1 from incompatible pointer type"); older compilers only warn. Multidimensional Arrays shows variable-length array parameters, which let the column count be a runtime value, and Pointers to Pointers covers the int ** layout.

Arrays are not pointers

Decay makes arrays and pointers look interchangeable, but some differences never go away:

Array int a[5] Pointer int *p
What it stores 5 ints one address
sizeof 20 (whole array) 8 (the pointer)
Can be assigned (a = ...)? No Yes, p = a or p = NULL
Can be incremented (a++)? No Yes
& gives int (*)[5] int **

An array name isn't a variable you can reassign; it is the storage. That's why int a[5], b[5]; a = b; fails to compile, and why you copy arrays with a loop or memcpy.

Strings show the difference clearly:

C
char greeting[] = "hello";       /* an array of 6 chars, a writable copy */
const char *motto = "hello";     /* a pointer to a read-only string literal */

greeting[0] = 'J';               /* fine: "Jello" */
motto = "goodbye";               /* fine: the pointer now points elsewhere */
/* motto[0] = 'J'; would be undefined behavior: string literals are read-only */
/* greeting = "goodbye"; does not compile: arrays can't be assigned */

Declaring the pointer as const char * makes the compiler reject motto[0] = 'J'. Without const, C would accept the write and the program would most likely crash at run time, because literals usually live in read-only memory. Strings covers this in more depth.

extern declarations must match. If one file defines int table[100]; and another declares it as extern int *table;, the program compiles and links but reads garbage: the second file treats the first 8 bytes of the array as an address. Declare it as extern int table[]; instead. It's a classic multi-file bug, covered in Multi-File Programs.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Given int a[4] = {2, 4, 6, 8}; int *p = a + 1;, what is p[2]?

    p[2] means *(p + 2). p already points at a[1], so p + 2 points at a[3], which is 8.
  2. 2. A function is declared void f(int values[50]). What is sizeof values inside it on a 64-bit system?

    An array parameter is adjusted to a pointer, so values is an int * and its size is the size of a pointer, 8 bytes. GCC warns about exactly this.
  3. 3. For int a[5]; with 4-byte ints, how many bytes apart are &a and &a + 1?

    &a has type int (*)[5], a pointer to the whole array, so adding 1 skips one whole array of 5 ints: 20 bytes.
  4. 4. Which is the correct parameter type for receiving int grid[3][4]?

    grid decays to a pointer to its first row, an int[4], so the type is int (*)[4] (also writable as int grid[][4]). int *grid[4] would be an array of pointers, and int ** is a different memory layout.
  5. 5. Why does int a[3], b[3]; a = b; fail to compile?

    An array name designates the array's storage itself, not a variable holding an address, so it can't appear on the left of =. Use a loop or memcpy(a, b, sizeof a).

Summary

  • In most expressions an array decays to a pointer to its first element: int[5] becomes int *.
  • a[i] is defined as *(a + i), so subscripts work on pointers and arrays alike.
  • Arrays don't decay under sizeof, under &, when a string literal initializes a char array, and (C23) under typeof.
  • sizeof array / sizeof array[0] gives the length only for real arrays, never for pointers.
  • Array parameters are pointers: the function changes the caller's array and needs the length passed separately. GCC warns about sizeof on an array parameter.
  • a and &a share an address but differ in type: int * versus int (*)[5].
  • A 2D array int g[R][C] decays to int (*)[C], so functions take int g[][C], never int **.
  • Arrays can't be assigned, incremented or returned; pointers can be reassigned freely.

Next: Pointers to Pointers