Search code examples
cgccunary-operatoraddressof

Why does GCC define unary operator '&&' instead of just using '&'?


As discussed in this question, GCC defines nonstandard unary operator && to take the address of a label.

Why does it define a new operator, instead of using the existing semantics of the & operator, and/or the semantics of functions (where foo and &foo both yield the address of the function foo())?


Solution

  • Label names do not interfere with other identifiers, because they are only used in gotos. A variable and a label can have the same name, and in standard C and C++ it's always clear from the context what is meant. So this is perfectly valid:

    name:
      int name;
      name = 4; // refers to the variable
      goto name; // refers to the label
    

    The distinction between & and && is thus needed so the compiler knows what kind of name to expect:

      &name; // refers to the variable
      &&name; // refers to the label