What is 0[p] doing?
Categories:
Demystifying 0[p]: Understanding Pointer Arithmetic in C
![Hero image for What is 0[p] doing?](/img/b178f773-hero.webp)
Explore the intriguing C expression 0[p]
and uncover how it leverages pointer arithmetic to access array elements, often leading to confusion but demonstrating C's flexibility.
In C programming, array indexing is typically performed using the syntax p[0]
, where p
is a pointer or an array name, and 0
is the index. However, you might occasionally encounter the peculiar expression 0[p]
. While it looks unconventional, 0[p]
is perfectly valid C and produces the same result as p[0]
. This article delves into the mechanics behind this syntax, explaining why it works and what it reveals about C's pointer arithmetic.
The Equivalence of Array Indexing and Pointer Arithmetic
The key to understanding 0[p]
lies in how C defines array indexing. In C, the expression a[i]
is formally defined as *(a + i)
. This means that accessing the i
-th element of an array a
is equivalent to dereferencing the memory address obtained by adding i
to the base address of a
. This definition is fundamental to C's memory model and pointer operations.
flowchart LR A[Array 'arr'] --> B{Base Address of arr} B -- + i --> C{Address of arr[i]} C -- Dereference --> D[Value of arr[i]] subgraph "arr[i]" B -- + i --> C end subgraph "*(arr + i)" B -- + i --> C end
Visualizing array indexing as pointer arithmetic
Given this definition, let's substitute a
with p
(a pointer) and i
with 0
. The standard p[0]
becomes *(p + 0)
. Now, consider 0[p]
. Applying the same definition, 0[p]
becomes *(0 + p)
. Due to the commutative property of addition, p + 0
is the same as 0 + p
. Therefore, *(p + 0)
is equivalent to *(0 + p)
, which means p[0]
is equivalent to 0[p]
.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr; // p points to the first element of arr
printf("Using p[0]: %d\n", p[0]);
printf("Using 0[p]: %d\n", 0[p]);
printf("Using p[2]: %d\n", p[2]);
printf("Using 2[p]: %d\n", 2[p]);
return 0;
}
Demonstrating the equivalence of p[i]
and i[p]
When you run this code, you'll observe that both p[0]
and 0[p]
print 10
, and p[2]
and 2[p]
print 30
. This confirms their identical behavior.
Why Does This Exist? (And Why You Should Avoid It)
The existence of 0[p]
(or i[p]
in general) is a direct consequence of C's flexible and low-level approach to memory and pointers. It's not a special feature but rather a side effect of how array indexing is implemented using pointer arithmetic and the commutative property of addition. While technically valid, 0[p]
is rarely seen in production code and is generally considered bad practice for several reasons:
0[p]
is its poor readability. It goes against conventional C syntax and can be confusing for developers unfamiliar with this specific quirk, making code harder to understand and maintain.0[p]
is valuable for deepening your comprehension of C's underlying memory model and pointer arithmetic. It highlights the flexibility and power of C, even if the specific syntax isn't recommended for daily use.In summary, 0[p]
is a valid C expression that works due to the definition of array indexing a[i]
as *(a + i)
and the commutative property of addition. While it's an interesting demonstration of C's flexibility, it should be avoided in practical programming in favor of the more conventional and readable p[0]
.