• kevincox@lemmy.ml
    link
    fedilink
    English
    arrow-up
    18
    arrow-down
    1
    ·
    2 days ago

    declaring multiple variables is less error-prone than in C. In C, the following declares x to be a pointer, but (surprisingly at first!) y to be a normal integer:

    int* x, y;
    

    Whereas the equivalent in Go does what you’d expect, declaring both to be pointers:

    var x, y *int
    

    I don’t think this is a related at all. C could have easily decided that the definition makes both x and y pointers. They just decided not to so that you can declare more variables on one line by being able to do int x, *y, **z, .... It is more flexible.

    Similarly that Go line could have been parsed like var x, (y*) int if they wanted to. They just made a different choice.

    • mEEGal@lemmy.world
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 day ago

      That’s because declaring several variables on the same line is syntactic sugar, although I agree it makes little sense this way

    • jdr@lemmy.ml
      cake
      link
      fedilink
      English
      arrow-up
      8
      arrow-down
      1
      ·
      2 days ago

      That’s why you write

      int *x, y, **z;

      C doesn’t write like “x is an int-pointer”, rather it says “dereferencing x will get you an int”.