6

This question is in response to What is &&& operation in C which got me thinking about & being used both as binary AND and address-of.

Why is the same symbol used for both these very dissimilar tasks? When thinking about it, @ would seem to be a better symbol for address-of.

I don't expect it to cause much problem in practice since the compiler will probably catch most faulty use, but it would probably be possible to create code involving macros that looks like it is doing a binary AND while it is actually doing address-of.

Community
  • 1
  • 1
Leo
  • 2,328
  • 2
  • 21
  • 41

2 Answers2

7

I don't think there is any big reason why it's used for 2 different things.

Luckily, there is no ambiguity in the language because one use is a unary operator and the other one is a binary operator (i.e. has two operands). Ditto for * (multiplication or dereference).

tom
  • 18,953
  • 4
  • 35
  • 35
7

The reason @ was not used for "address of" (as you suggested would be a good choice) is that that character was not part of the ISO 646 character set, which was actually used back in the early days of C.

While it is true that the designers of C could have used @ and designed a trigraph sequence for it, I suspect they felt it was not really worth the hassle. The fact that &&& can appear in people's code was probably not considered reason enough to choose a different operator symbol.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Agreed, the only reason for any ambiguity for humans is people *trying* to be clever by omitting whitespace and using obscure language features, rather than actually *being* clever... – thkala Dec 20 '12 at 09:35
  • +1: I expected there to be some historical reason. A more limited character set explains why the same characters are used for multiple meanings. It was not `&&&` I had in mind when questioning why `&` has multiple uses though. More something like `a & b` where `a` ends up being set by `#define a`. This looks like a `AND` but is in fact not. Hopefully the compiler can spot the mistake, but that depends entirely on what you use the result for. – Leo Dec 20 '12 at 10:43