简体   繁体   中英

Why is `&` used for both binary AND and address-of in C?

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.

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 (ie has two operands). Ditto for * (multiplication or dereference).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM