Arrays vs Pointers
An array is a block of objects. A pointer is the address of one object. In most expressions the array becomes a pointer to its first element, which is why the two feel interchangeable and then suddenly are not.
Detailed Explanation
int a[4]; reserves four ints. sizeof a is 4 * sizeof(int). int *p = a; stores the address of a[0]. sizeof p is the size of a pointer. After that assignment, a[i] and p[i] read the same bytes, and p + i is the same address as &a[i]. The sameness is about access, not about the types.
The conversion is called decay. It happens when the array is used in an expression, including as a function argument. It does not happen for sizeof, &, or _Alignof. &a has type int (*)[4], a pointer to the whole array. &a + 1 skips four ints. p + 1 skips one. That difference is the usual way to catch a function that took the wrong kind of pointer.
A function parameter declared int a[4] or int a[] is rewritten to int *a. There is no array parameter. The length is lost. Passing a to a function is passing a pointer, and the callee cannot recover sizeof of the original array. Pass a size_t n next to it.
A pointer can address one object, an element of an array, a block from malloc, or nothing (NULL). An array name cannot be NULL and cannot be reassigned. a = p; is a constraint violation. p = a; is normal. p = NULL; is normal.
Heap "arrays" are pointers. int *p = malloc(n * sizeof *p); is a pointer to the first of n objects. It is not type int[n]. You free it with free(p). You never free(a) for a stack or static array.
If you need both the pointer and the length to travel together, put them in a struct: { int *data; size_t len; }. That is the C version of a slice, and it stops the length from getting lost at every call.
Code Examples
#include <stdio.h>
static void show(int *p, size_t n)
{
printf("param sizeof=%zu n=%zu first=%d\n", sizeof p, n, p[0]);
}
int main(void)
{
int a[4] = { 1, 2, 3, 4 };
printf("array sizeof=%zu\n", sizeof a); /* typical: 16 */
show(a, sizeof a / sizeof a[0]);
/* typical: param sizeof=8 n=4 first=1 */
int *p = a;
printf("%d\n", *(p + 2)); /* prints: 3 */
return 0;
}#include <stdio.h>
int main(void)
{
int a[4] = { 0 };
int *p = a;
int (*row)[4] = &a; /* pointer to the whole array */
printf("%td %td\n",
(p + 1) - p, /* 1 int */
(int *)(row + 1) - (int *)row); /* 4 ints */
/* prints: 1 4 */
return 0;
}Common Gotchas
`sizeof` a parameter that "looks like" an array is the size of a pointer
passing `sizeof a` from inside that function as a byte count is a buffer overflow waiting to happen.
`&a` and `a` are different types that often have the same address
pointer arithmetic on them does not move the same distance.
`NULL` is a pointer value, not an array state
an array object always exists if its declaration did. A pointer to a heap block can be null after a failed `malloc`.
You may `free` a `malloc` pointer, never an array name
the converse, using `sizeof` on the pointer as if it were the block, is just as wrong.