• @[email protected]
    link
    fedilink
    5510 months ago

    The fact it’s a pointer is part of the type, not part of the variable name. So int* p is the way.

    • @[email protected]
      link
      fedilink
      82
      edit-2
      10 months ago

      You would think so, but int* a, b is actually eqivalent to int* a; int b, so the asterisk actually does go with the name. Writing int* a, *b is inconsistent, so int *a, *b is the way to go.

      • @[email protected]
        link
        fedilink
        3510 months ago

        When people say “pointers are hard”, they mean “I have no idea where the star goes and now an ampersand is also implicated”.

        • @T156
          link
          English
          2310 months ago

          That’s the part where you give up and randomly shove/unshove symbols in until the code works.

      • @[email protected]
        link
        fedilink
        2310 months ago

        While technically true, that’s also one of the worst ‘features’ of the language and I personally consider it a bug in the language. Use two lines and make it clear and correct.

      • ono
        link
        fedilink
        English
        310 months ago

        This is true in C, but not in D.

    • sham1
      link
      fedilink
      1610 months ago

      Then again, at least in C, the mantra is “declaration follows usage”. Surely you don’t write pointer dereferences as * ptr? Most likely not, you most likely write it as *ptr. The idea behind the int *ptr; syntax is basically that when you do *ptr, you get an int.

      And with this idea, stuff like function pointers (int (*f)(void)), arrays of pointers (int *a[10]) versus pointers of arrays (int (*a)[10]) etc. start making sense. It’s certainly not the best way to design the syntax, and I’m as much a fan of the Pascal-styled “type follows the identifier” syntax (e.g. let x: number;) as anyone, but the C way does have a rhyme and a reason for the way it is.

    • @marcos
      link
      13
      edit-2
      10 months ago
      int* i, j
      

      The C syntax is just messed up.

    • Traister101
      link
      fedilink
      5
      edit-2
      10 months ago

      It’s part of the type yet it’s also a unique identifier. That’s the whole thing with east or west const. const int * is a immutable mutable pointer that points to mutable immutable memory. int *const is a mutable immutable pointer that points to immutable memory. int const * is the same type as the first example, a immutable mutable pointer that points to mutable immutable memory.

      Same stuff applies to references which makes it easier to think of the variable owning the * or & as if you want that pointer or reference to be const it has to go after.

      Edit:I am a moron who managed to get it exactly backwards :|

        • @SpaceNoodle
          link
          410 months ago

          I wrote a couple unholy lines of C++ the other day using the ternary conditional operator to select a class member function to be called with a fixed argument.

          I think my teammates were too scared to call me out on it.

            • @SpaceNoodle
              link
              310 months ago

              It’s actually simpler than doing it the “right” way, but I wanted to see how much I could make C++ pretend it was Python.

        • Traister101
          link
          fedilink
          310 months ago

          Lol yeah. I don’t even really write C++ but I sure as shit know a bunch of syntax and junk haha

      • @[email protected]
        link
        fedilink
        English
        310 months ago

        I think you’ve got it backwards. I learned to read pointer decls from right-to-left, so const int * is a (mutable) pointer to an int which is const while int *const is a const pointer to a (mutable) int.

        • Traister101
          link
          fedilink
          210 months ago

          Fuck me man that’s what I get for writing that just before bed

    • @[email protected]
      link
      fedilink
      410 months ago

      I do this in my code because it looks better and makes more sense…until I decide to declare 2 vars on one line and then I use the very cursed int* a, *b

    • @owsei
      link
      310 months ago

      tbh I always think about it as ‘p’ is a pointer to int

      therefore *p is an int

      therefore I should call it int *p;

      however, of course, you should use what your team prefers. Having good yet inconsistent style is worst than mid consistent style.

    • @ShortFuse
      link
      2
      edit-2
      10 months ago

      I don’t code much C++, but then I’d lose alignment with: x = *p; and I feel that would bug me.

      I’m looking at Google Style Guide for my next project and it says either is fine, just don’t declare more than one per line.

    • @SpaceNoodle
      link
      210 months ago

      And yet the default clang formatter gets it wrong.