Lesson 3 of 6
Array-Pointer Duality - When Arrays Decay
In C, an array name **decays** to a pointer to its first element in almost all contexts. `int arr[5]` decays to `int *` when used in expressions. However, decay does NOT happen in three situations: when used as the operand of `sizeof`, when used as the operand of `&` (address-of), and when used as a string literal initialiser for a `char` array. Understanding when decay happens (and doesn't) prevents both bugs and misconceptions.
Array Decay - When It Happens and When It Doesn't
#include <stdio.h>
#include <string.h>
void print_array(int *arr, int n) {
/* Here 'arr' is already a pointer - sizeof gives pointer size */
printf("sizeof inside function: %zu
", sizeof(arr)); /* 8 - POINTER! */
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("
");
}
int main(void) {
int arr[5] = {1, 2, 3, 4, 5};
/* ── When decay HAPPENS ───────────────────────────────── */
/* 1. In most expressions - arr becomes &arr[0] */
int *p = arr; /* arr decays to int* pointing to arr[0] */
printf("arr[2] = %d
", arr[2]); /* subscript notation */
printf("*(p+2) = %d
", *(p+2)); /* pointer notation - same thing */
printf("p[2] = %d
", p[2]); /* pointer subscript - also same */
/* In C: arr[i] is DEFINED as *(arr + i) */
/* 2. When passed to a function - decays to pointer */
print_array(arr, 5); /* arr decays to int* here */
/* ── When decay does NOT happen ──────────────────────── */
/* 1. sizeof(arr) - gives FULL array size, not pointer size */
printf("sizeof(arr) outside = %zu
", sizeof(arr)); /* 20 (5*4) */
/* 2. &arr - address-of the ARRAY, not of the first element */
int (*parr)[5] = &arr; /* pointer to an array of 5 ints */
printf("&arr type: pointer to array[5], value=%p
", (void *)parr);
/* parr+1 advances by sizeof(int[5]) = 20 bytes
&arr[0]+1 advances by sizeof(int) = 4 bytes */
/* 3. String literal initializing char array - no decay */
char str[] = "hello"; /* "hello" initializes arr, doesn't decay */
printf("sizeof(str) = %zu
", sizeof(str)); /* 6 - full array */
/* ── Equivalence: subscript and pointer notation ─────── */
/* These are all identical for arrays and pointers: */
printf("arr[3] = %d
", arr[3]); /* 4 */
printf("*(arr+3) = %d
", *(arr+3)); /* 4 */
printf("*(3+arr) = %d
", *(3+arr)); /* 4 - addition is commutative */
printf("3[arr] = %d
", 3[arr]); /* 4 - legal C, just weird */
return 0;
}Common Mistakes
- Using `sizeof(arr)` inside a function receiving an array parameter - the parameter is a pointer, not an array. `sizeof` gives pointer size (8 bytes). Always pass length separately.
- `&arr` vs `arr` - both have the same numeric value (the address of the first element) but different types: `arr` decays to `int*`, while `&arr` is `int(*)[5]`. Adding 1 to each advances by different amounts.
- Thinking arrays and pointers are the same - an array IS a region of memory. A pointer IS a variable holding an address. They behave the same in many contexts due to decay, but they are fundamentally different.
Tip
Tip
Practice ArrayPointer Duality When Arrays Decay in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Array names decay into pointers to their first element when passed into functions
Practice Task
Note
Practice Task - (1) Write a working example of ArrayPointer Duality When Arrays Decay from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with ArrayPointer Duality When Arrays Decay is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- In C, an array name **decays** to a pointer to its first element in almost all contexts.
- Using `sizeof(arr)` inside a function receiving an array parameter - the parameter is a pointer, not an array. `sizeof` gives pointer size (8 bytes). Always pass length separately.
- `&arr` vs `arr` - both have the same numeric value (the address of the first element) but different types: `arr` decays to `int*`, while `&arr` is `int(*)[5]`. Adding 1 to each advances by different amounts.
- Thinking arrays and pointers are the same - an array IS a region of memory. A pointer IS a variable holding an address. They behave the same in many contexts due to decay, but they are fundamentally different.
Finished this lesson?