you are viewing a single comment's thread
view the rest of the comments
[–] 59 points 2 years ago (8 children)

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

  • source
  • hideshow 16 child comments
  • [–] 86 points 2 years ago* (6 children)

    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.

  • source
  • parent
  • hideshow 12 child comments
  • [–] 38 points 2 years ago (1 child)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 16 points 2 years 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.

  • source
  • parent
  • [–] 6 points 2 years ago* (last edited 2 years ago) (3 children)

    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 :|

  • source
  • parent
  • hideshow 6 child comments
  • [–] 7 points 2 years ago (2 children)

    Found the guy that can probably do function pointers!

  • source
  • parent
  • hideshow 4 child comments
  • [–] 4 points 2 years ago (1 child)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 2 years 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.

  • source
  • parent