Skip to content
DevMeme
1389 of 7435
C++ Pointer Arithmetic: A Corporate Investigation
Languages Post #1562, on May 11, 2020 in TG

C++ Pointer Arithmetic: A Corporate Investigation

Why is this Languages meme funny?

Level 1: Same Box, Different Order

Imagine you have a long row of toy boxes, and each box has a number on it (0, 1, 2, 3, ... in order). Now, suppose you want the toy in box number 3. You could tell your friend, “Go to box 3 and get the toy.” But you could also say, “Get me the 3rd box in the row.” Those sound a bit different, yet they lead to the exact same box and the exact same toy, right? The order of the words changed, but which toy you meant didn’t change at all.

This meme is joking that the C programming language thinks about array positions just like that. Whether you say it the usual way (“box 3 from the row”) or you flip it around (“the row’s 3rd box”), C will fetch the same toy from the same box. The computer doesn’t get confused by the reversed order – it knows you meant the box with that number either way. It’s a funny surprise because we don’t normally talk like that, but the C language is totally fine with it. So the two pictures with Array[i] and i[Array] are like two labels for the same toy box. To the C compiler, they’re the same picture, and that’s what makes programmers laugh in disbelief and delight!

Level 2: No Differences Found

This meme references a scene from The Office where the character Pam is asked to find differences between two images that are actually identical. In the meme’s version, the two “pictures” being compared are the snippets of C code Array[i] and i[Array]. The joke is that C (represented by the big blue C language logo covering Pam’s face) looks at these two seemingly different pieces of code and says, “They’re the same picture.” In other words, the C compiler finds no difference between Array[i] and i[Array]. This is a funny twist because, to a person, i[Array] looks backwards or just wrong at first glance – we usually always write the array name on the left! But in C, due to how arrays and pointers work, swapping the order doesn’t change the meaning. Corporate asked for a difference, but C couldn’t find any.

Let’s break down what’s happening in simpler terms. In C programming (one of the classic CFamilyLanguages), an array is essentially a consecutive block of items in memory, and a pointer is a variable that holds a memory address. When you use an array in a C expression, it often “decays” to a pointer to its first element. That means if you have an array called Array, you can think of Array (in most contexts) as if it were a pointer to the start of that array’s memory. For example, if Array starts at memory address 1000, then Array can act like the pointer 1000 (the address of element 0).

Now, the subscript operator in C is written with square brackets [...]. When you see something like X[Y], C interprets this as “take the pointer X, move Y elements forward from that pointer, and get whatever is at that new location.” Typically, we put the array name on the left side and the index on the right side, e.g. Array[i], which means “start at the beginning of Array and go forward i elements.” This is how we access the i-th element (remember, C uses zero-based indexing, so Array[0] is the first element, Array[1] the second, and so on).

Here’s the wild part: C doesn’t actually require the pointer to be on the left of the bracket and the integer on the right – it just needs one of them to be a pointer and one to be an integer. The addition inside the bracket can be done in either order. So Array[i] means “take pointer Array + integer i, and i[Array] means “take integer i + pointer Array. But adding an integer to a pointer or a pointer to an integer ends up the same place in memory! It’s just like how 5 + 2 is the same as 2 + 5 in normal arithmetic – the order doesn’t matter for addition. Here, Array acts like a pointer value (the start address), and i is an integer offset. So whether you do start + offset or offset + start, you end up at the same address. When you then dereference that address (i.e., look up the actual array element stored there), it’s the same element.

To visualize this: imagine Array points to address 1000. If each element is 4 bytes (say, they’re int), then Array[2] moves 2 elements forward, landing at address 1008, and gives you whatever is stored at 1008. If you wrote 2[Array], the compiler interprets it as *(2 + Array). Starting from Array (1000) and adding 2 gets you to 1008 as well – the same address. So 2[Array] will fetch what’s at 1008 too. No differences found! The commutative_subscript_operator means the compiler will end up in the same spot, no matter the order of the terms.

For a newer programmer or someone not familiar with C, i[Array] just looks wrong. In most languages (and in typical code), the convention is always arrayName[index]. If you tried i[arrayName] in something like Java or Python, you’d get a big error – those languages expect the left side to actually be an array or list type. An integer on the left side doesn’t make sense to them. But C is a lower-level language with simple rules: it doesn’t actually have a dedicated “array” type at runtime – an array mostly behaves like a pointer to a block of memory. The [] operator is basically just pointer math in disguise. This is why C (and C++ by extension) allows this little oddity. It’s a LowLevelProgramming detail that every element Array[i] is computed by adding i to a base address. The order of addition isn’t enforced.

The meme capitalizes on this by using The Office reference: Pam is confronted with two codes that a human might think are different, but as the embodiment of the C compiler she identifies no difference whatsoever. The phrase “They’re the same picture” in the subtitle is exactly what a C compiler would say if it could speak – Array[i] and i[Array] compile to the same thing. It highlights a piece of CodingHumor where a language’s quirk aligns perfectly with a pop culture reference. For developers, it’s both funny and educational: a reminder that C lets you do things in weird orders sometimes. After seeing this meme, a junior developer might even try out i[arr] in a toy program just to see it with their own eyes! And indeed, they’d find that C treats the two ways of indexing as one and the same. In summary: in C, the difference between Array[i] and i[Array] doesn’t exist – and that’s exactly the punchline.

Level 3: Subscript Shenanigans

In C and C++, the expression Array[i] is literally identical in meaning to i[Array] – a fact that feels like low-level pointer arithmetic magic. The meme humorously shows two “different” pictures (Array[i] on one paper and i[Array] on another) and asks us to spot the difference. The blue-and-white C language logo (playing the role of Pam from The Office) then says, “They’re the same picture.” This is a nod to a classic LanguageQuirk: the commutative subscript operator. In C, the compiler genuinely treats Array[i] and i[Array] as the same code under the hood. Seasoned developers in CFamilyLanguages chuckle at this because it’s a fundamental truth of how arrays work in C – one that surprises newbies and delights LowLevelProgramming geeks.

But why are they the same? In C, array indexing is defined in terms of pointer arithmetic. The notation a[b] is defined by the language to mean “take the pointer a, add b to it (in units of the element size), and then dereference the result.” In other words, a[b] is evaluated as *(a + b). And because addition is commutative (i.e. a + b equals b + a), you can flip the operands: *(a + b) is the same as *(b + a). Writing that back in subscript form gives *(b + a) => b[a]. So a[b] and b[a] compile to the exact same machine instructions. The C compiler essentially says, “Array index or index array – whatever, it’s all just pointer math to me.” This is why the meme shows the C compiler unable to find any difference between the two pictures.

Let’s look at a quick example of these PointerArithmetic shenanigans in action:

#include <stdio.h>

int main() {
    int Array[5] = {10, 20, 30, 40, 50};
    int i = 2;
    printf("%d\n", Array[i]);   // prints 30 (the element at index 2)
    printf("%d\n", i[Array]);   // also prints 30, because i[Array] == Array[i]
    return 0;
}

Both Array[i] and i[Array] will print the same value (30 in this case). That’s because both lines are doing the same thing: accessing the third element of the array (index 2 in zero-based indexing). The expression Array[i] is evaluated by the compiler as *(Array + i). The expression i[Array] is evaluated as *(i + Array). And since Array + i and i + Array describe the same pointer address, both lines access *(Array + 2) – the integer at that memory location. We could even illustrate this equivalence explicitly:

// These are all equivalent in C:
Array[i];
*(Array + i);
*(i + Array);
i[Array];

Yes, you’re reading that correctly – you can literally write i[Array]! It looks bizarre, but it’s 100% valid C. This isn’t a hack or undefined behavior; it’s baked into the language specification. As long as one of the two terms in the brackets is a pointer (or array that decays to a pointer) and the other is an integer, the compiler is happy. The meme gets its punchline from this exact rule: the C compiler (with its decades-old design decisions in CS_Fundamentals) doesn’t distinguish between the two forms. It sees them as the same memory address dereference – hence Pam’s deadpan “They’re the same picture.”

This quirky equality often blows the minds of newcomers. It’s a beloved piece of C trivia that senior developers joke about. In fact, clever C programmers sometimes demonstrate it in playful ways. For example, you might see something crazy like:

printf("%c\n", 2["ABC"]);  // prints 'C'

Believe it or not, 2["ABC"] is the same as "ABC"[2], which accesses the character 'C' in the string! This works because the string literal "ABC" gives us a pointer to its first character, and then we add 2 to that pointer. It’s an absurd-looking trick that highlights how low-level programming in C lets you bend the usual reading order. No matter whether you write it as Array[i] or i[Array], the C compiler will shrug (much like Pam) and happily produce code to fetch the same element.

Why is this funny to developers? It’s the contrast between human expectation and machine logic. Humans expect order to matter – we normally say “array, then index.” But the machine only sees pointer addresses and numeric offsets. This commutative property of the subscript operator is a reminder that, under the hood, C treats arrays in a very primitive way. It’s both CodingHumor and a brain-teaser: a weird little feature that makes you do a double-take. Seasoned C developers share a knowing grin about it, and might even use it as a light-hearted interview question or an example of C’s eccentric elegance. Just remember: while i[Array] will earn you geek cred on a forum or a DeveloperHumor thread, you probably shouldn’t start writing your code that way at work – unless you want your coworkers to perform a real-life “spot the difference” code review on you! 😄

Description

This meme uses the popular 'Corporate needs you to find the differences between this picture and this picture' format from the TV show 'The Office.' In the top panel, the character Pam holds up two images. The left one displays the code 'Array[i]', the standard syntax for accessing an array element. The right one shows 'i[Array]', a highly unconventional but valid syntax. In the bottom panel, Pam's face is obscured by the C++ logo, and she declares, 'They're the same picture.' The humor is rooted in a deep-cut feature of C and C++. Due to how pointer arithmetic is implemented, `Array[i]` is interpreted by the compiler as `*(Array + i)`. Since addition is commutative, this is identical to `*(i + Array)`, which can be expressed as `i[Array]`. Thus, both expressions are functionally the same. This esoteric language quirk is often used as a trivia question to test a developer's understanding of the language's low-level mechanics

Comments

7
Anonymous ★ Top Pick Using `i[Array]` in production code is the professional equivalent of putting a mint in your urinal: technically it works, but it's going to make everyone deeply uncomfortable
  1. Anonymous ★ Top Pick

    Using `i[Array]` in production code is the professional equivalent of putting a mint in your urinal: technically it works, but it's going to make everyone deeply uncomfortable

  2. Anonymous

    I slip an occasional i[Array] into the C codebase - if the PR gets rubber-stamped, I know the reviewers are parsing with their eyes, not their pointers

  3. Anonymous

    After 20 years of explaining to junior devs why i[Array] works in C, you realize the real bug is that you still remember the segment:offset addressing from your 8086 days and occasionally dream in assembly

  4. Anonymous

    Ah yes, the classic C interview gotcha that separates those who've actually read K&R from those who just use arrays like normal people. Sure, 'i[Array]' works because array subscripting is just pointer arithmetic in disguise - *(Array + i) equals *(i + Array) thanks to commutative addition - but if I ever see this in production code, we're having a very different conversation about maintainability. It's the programming equivalent of technically-correct-but-please-don't: like using trigraphs or #define true false. Yes, you've demonstrated deep knowledge of C's memory model. No, your code reviewers won't thank you for flexing it

  5. Anonymous

    C defines a[b] as *(a + b), so i[arr] compiles; the compiler approves, but your readability SLO just breached

  6. Anonymous

    In C, i[arr] == arr[i] - pointer addition commutes; bounds are a social construct

  7. Anonymous

    Corporate wants diffs; C's preprocessor says they're identical after *(Array + i)

Use J and K for navigation