开发者

Official names for pointer operators

开发者 https://www.devze.com 2022-12-23 11:55 出处:网络
What are the official names for the operators * and & in the context of pointers? They seem to be frequently called dereference operator and address-of operator respectively, but unfortunately, th

What are the official names for the operators * and & in the context of pointers? They seem to be frequently called dereference operator and address-of operator respectively, but unfortunately, the section on unary operators in the standard does not name them.

I really don't want to name & address-of anymore, because & returns a pointer, not an address. (see below) The standard is very clear about this:

The result of the unary & operator is a pointer to its operand.

Symmetry suggests to name & reference operator which is a little unfortunate because of the collision with references in C++. The fact that & returns a pointer suggests pointer operator. Are there any official sources that would confirm these (or other) namings?

pointers vs. addresses

A pointer is a language mechanism, while an address is an implementation detail. Addresses are unt开发者_StackOverflow社区yped, while pointers aren't, except for void*. Kevlin Henney also distinguishes between pointers and addresses in an Interview:

C [...] allows us to abstract the specifics of the machine to the point that we are talking about pointers and not addresses. There is a whole load of pain that you no longer have to go through.


From the C99 draft, at the index:

* (indirection operator), 6.5.2.1, 6.5.3.2

& (address operator), 6.3.2.1, 6.5.3.2

From the C++0x draft, at the index:

*, see indirection operator, see multiplication operator

&, see address-of operator, see bitwise AND operator

It's also referenced in 9.6/3 "The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields."

(So, sorry, you still need to call & "address-of" :p)

Personally I don't care the actual name as long as other can understand what I'm saying. I just call * "star" and & "and". :)


The official names are address-of (&) (Found in 2.3.3. Pointers and Arrays) and dereference (or indirection operator) (*) (Found in 5.1.Pointers) operators.

according to "The C++ Programming Language", Third Edition by Bjarne Stroustrup.


Not true.

& does not return a pointer.

A pointer is a variable of pointer type, that may be used to contain address. Pointer has an address of it's own, usually different from its value, which is the address it holds.

As such, pointer is lvalue, and result of &p is not.

Example:

int *p, a, b;

p = &b; // valid, p is a pointer
&a = &b; // invalid, address is not a variable. lvalue required
0

精彩评论

暂无评论...
验证码 换一张
取 消