• kevincox@lemmy.ml
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 hours 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.

    • jdr@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      58 minutes 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”.