c language · high
C: Pointers vs Arrays (Decay)
In C an array and a pointer are distinct types, though an array name usually 'decays' to a pointer to its first element in expressions — which is why int* p = a; needs no &. The array object still knows its full size, so sizeof(a) is 20 for int a[5] while sizeof(p) is just pointer size; decay does not occur for sizeof, &array, or _Alignof. A crucial gotcha: an array function parameter is silently rewritten to a pointer, so void f(int x[5]) is exactly void f(int* x) — the size is ignored, sizeof(x) inside is pointer size, and the element count cannot be recovered, so you must pass the length separately. Finally, a and &a have the same address but different types: a decays to int* while &a is int(*)[5] (pointer to the whole array), so a+1 advances by one int (4 bytes) but &a+1 advances by the whole array (20 bytes) — reasoning about the pointed-to type, not just the value, is the key to C pointer questions.
C arrays are not pointers: an array decays to &a[0] in expressions but sizeof on the array gives total bytes; array function parameters silently become pointers (size ignored — pass length); a and &a share an address but differ in type (a+1 vs &a+1).
The code
int a[5];sizeof(a); // 20 (5 * sizeof(int)) — array knows its sizeint* p = a; // array DECAYS to &a[0]sizeof(p); // 8 — just a pointer now
void f(int x[5]) { // the '5' is a lie: parameter is int* x sizeof(x); // 8 (pointer), NOT 20}&a; // type int(*)[5], value == a but different typeWhat this lesson walks through
- 01An array is not a pointer
- 02Assigning to a pointer DECAYS the array
- 03Gotcha — a function parameter's size is fiction
- 04&array vs array — same address, different type
int a[5] is 5 ints laid out contiguously — 20 bytes — and the array name knows that size. A pointer is just one address (8 bytes). They are different types.
See it animated — step by step, at your own pace
Unlock the full interactive walkthrough of C: Pointers vs Arrays (Decay) and 100+ animated C++ interview lessons.