Skip to content
All articles

C Programming

Pointers and arrays in C: why does an array turn into a pointer?

When you pass an array to a function in C, you're actually passing the address of its first element. That small fact is behind many surprises, from the sizeof trap to buffer overflows. Here's how pointers and arrays relate.

Fahrettin Baştürk4 min read

For anyone learning C, the first big turning point is usually pointers. The question that immediately follows is: "Is an array the same as a pointer?" Some teachers say "yes, almost", others say "no, absolutely not". Both are partly right, and understanding the difference affects every line of C you'll write.

An array isn't a pointer, but it often acts like one

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;            // arr "decays" into the address of its first element

printf("%d\n", *p);       // 10
printf("%d\n", *(p + 2)); // 30
printf("%d\n", arr[2]);   // 30
printf("%d\n", p[2]);     // 30

In most expressions, an array automatically converts into a pointer to its first element. This is called "array decay". Writing arr[2] is really shorthand for *(arr + 2). But the array still isn't a pointer: it's a block of five integers in memory, while a pointer is just a variable holding an address.

Pointer arithmetic

p + 2 doesn't add 2 bytes to the address; it adds 2 *elements*. If int is 4 bytes, the address moves 8 bytes. The compiler works out the step size from the pointer's type. That's why the same operation gives different results on char* and int*.

The sizeof trap

The difference is clearest with sizeof:

int arr[5];
int *p = arr;

sizeof(arr);   // 20: the whole array (5 * 4)
sizeof(p);     // 8: just the pointer (on a 64-bit system)

void f(int a[5]) {
    sizeof(a);  // 8! the "array" parameter is really a pointer
}

Writing int a[5] in a function parameter changes nothing; the compiler sees it as int *a. The array's size never reaches the function. That's why C functions taking arrays take the length as a separate parameter:

int sum(const int *a, size_t n) {
    int t = 0;
    for (size_t i = 0; i < n; i++) t += a[i];
    return t;
}

sum(arr, sizeof(arr) / sizeof(arr[0]));   // the caller computes the element count

Arrays and overflows

C doesn't check array bounds. Writing arr[5] or arr[100] compiles and runs; it just reads or overwrites some other part of memory. That's undefined behavior; the program may keep running, and the bug surfaces somewhere else. Most segmentation faults and a large share of security vulnerabilities start right here.

Two-dimensional arrays

int m[3][4];           // 3 rows, each with 4 ints
int (*row)[4] = m;     // m decays into "pointer to an array of 4 ints"

A two-dimensional array is rows laid out back to back in memory. When passing it to a function you need to specify the column count (void f(int m[][4], size_t rows)), because the compiler has to know how many bytes a row is.

Why it matters

Understanding how pointers and arrays relate is the foundation for understanding strings in C (char arrays), dynamic memory and passing data to functions. In C++, tools like std::vector and std::span solve most of these problems, but the same memory model is underneath. If you want to take the next step in C++, my post on pointers vs references is a good continuation.

When I first learned pointers, I understood them by drawing boxes and arrows on paper. I still do the same when I face a complicated structure. Sometimes the best debugger is a pen and a sheet of paper.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C
  • Pointers
  • Arrays
  • Learning C

Working on a similar system or problem?Tell me about your project