Skip to content
DevMeme
5530 of 7435
Array Indexing vs. The Cooler Pointer Arithmetic
Languages Post #6068, on Jun 12, 2024 in TG

Array Indexing vs. The Cooler Pointer Arithmetic

Description

This image uses the 'Daniel vs. The Cooler Daniel' meme format, which consists of two side-by-side yearbook photos of the same person. The first, labeled 'Daniel', shows a standard, smiling portrait. The second, labeled 'The cooler Daniel', shows him wearing sunglasses and looking more suave. This format is used to compare two things, suggesting the second is a more stylish or sophisticated version of the first. In this tech-focused version, the code snippet 'a[i]=5' is superimposed over the first photo. This represents standard array bracket notation for element access, which is common and readable in many programming languages. Over the 'cooler Daniel' photo is the code '*(a+i)=5'. This represents pointer arithmetic, a more explicit way of accessing the same array element, common in languages like C and C++. In C/C++, these two syntaxes are functionally identical and compile to the same instructions. The humor lies in the developer in-joke that using explicit pointer arithmetic is a 'cooler', more hardcore, or more knowledgeable way to write code, even though it's less readable than the standard array notation. It pokes fun at the tendency among some programmers to favor more obscure or low-level syntax as a way of demonstrating technical prowess

Comments

34
Anonymous ★ Top Pick Modern compilers see no difference between the two. Modern code reviewers, however, see a significant difference in the author's desire to collaborate
  1. Anonymous ★ Top Pick

    Modern compilers see no difference between the two. Modern code reviewers, however, see a significant difference in the author's desire to collaborate

  2. Anonymous

    Sure, both compile to the same MOV instruction, but one of them makes the reviewer think you can read objdump output without coffee

  3. Anonymous

    The real cooler Daniel would've used a[i] = 5 with -O3 and let the compiler decide whether pointer arithmetic was actually faster, then spent the saved time arguing about move semantics in the code review

  4. Anonymous

    The real senior move is knowing that `a[i]` is literally just syntactic sugar for `*(a+i)` - the compiler generates identical assembly either way. But we all know the architect who insists on pointer arithmetic in code reviews because 'it shows you understand what's really happening,' while the rest of us are just trying to pass the readability bar and avoid off-by-one errors in production at 3 AM

  5. Anonymous

    Cooler Daniel thinks *(a+i)=5 is faster; the optimizer disagrees, but future-you debugging the inevitable off-by-one at 3am will remember who approved it

  6. Anonymous

    C defines a[i] as *(a+i); the optimizer emits identical assembly - only the PR nitpicks scale with the ‘coolness’

  7. Anonymous

    Pointer arithmetic: because who needs bounds checks when segfaults come with swagger?

  8. Philipp 2y

    Except for the MISRA violation

  9. @VanuxaKR 2y

    i[a]=5;

    1. @mihanizzm 2y

      Yesssssss

    2. Deleted Account 2y

      5=i[a];

      1. @VanuxaKR 2y

        And now this is an error

        1. Deleted Account 2y

          Unfortunately yes

      2. @azizhakberdiev 2y

        this is not what commutative property states

      3. @azizhakberdiev 2y

        but you can overload "=" to make it work

        1. Deleted Account 2y

          i'm not sure about operator overloading in C

          1. @azizhakberdiev 2y

            nobody said it is. *(a+i) works in C++ too

            1. Deleted Account 2y

              yeah. forgot about it

      4. @ahmubashshir 2y

        ah, I see, the Yoda notation...

    3. @ahmubashshir 2y

      NULL[a + i] = 5

      1. @VanuxaKR 2y

        error: array subscript is not an integer

        1. @ahmubashshir 2y

          yeah

        2. @ahmubashshir 2y

          another way is casting NULL to size_t

          1. @VanuxaKR 2y

            nah, casting isnt interesting for me

            1. @ahmubashshir 2y

              XD

      2. @VanuxaKR 2y

        but it will work with 0[a+i]...

  10. @Agent1378 2y

    Satan laughs in C/C++

  11. @Aleqsonder 2y

    Is *(a+i) more efficient than a[i]? I thought they are absolutely same

    1. @purplesyringa 2y

      they're the same

      1. @Aleqsonder 2y

        Ok, thanks. That meme made me doubt

    2. @mira_the_cat 2y

      imho a[i] could be more efficient

      1. @Aleqsonder 2y

        Why so?

  12. @graduated_vernier 2y

    Foot gun? More like foot cannon!

  13. @ahmubashshir 2y

    I forgot

Use J and K for navigation