* declares a pointer. I usually think "int pointer a" when writing int *a.
But then it gets confusing:
int b = *a; stands for "int b is value of a", not "pointer of a". And with int *a = &b the confusion then is perfect, as the new symbol & now stands for "pointer of b" again.
Does anyone know the secret logic behind this convention? Why doesn't * simply stand for "pointer" and & for "value"? Or can these two symbols be thought as other words, so that it makes more sense?
EDIT: How this question is different from the four following questions that have been linked as possible duplicates, highlighting the difference between the associated questions and mine
My question asks about
* and &* and & symbols can be thought / spoken in wordsthe linked ones don't. In addition to that, neither of the answers to these questions mention the page 90 of the 1978 K&R, where it sates that the declaration of the pointer is intended as a mnemonic. This is exactly what I was looking for.
The logic is this:
* always indicates dereferencing of a pointer.* causes the pointer to be dereferenced.* in a declaration tells us that dereferencing the identifier produces the declared type. (See more below.)In contrast, the = has different meanings in a declaration and an expression:
= after a declarator introduces initialization for the object being declared. It applies to the specific object being declared, not to the whole “expression” that is pictured.= means to assign a new value to the object on the left.So int *a; means “When *a appears in an expression, it is an int.” Therefore, it tells us that the type of a is a pointer to an int.
Some additional discussion is here and here.
int b = *a;stands for "int b is value of a", not "pointer of a".
int b = *a; says “b is an int, and initialize b with the value of *a.”
Note that *a is not the value of a. It is what a points to.
And with
int *a = &bthe confusion then is perfect, as the new symbol&now stands for "pointer of b" again.
int *a = &b says “*a is an int, so the type of a is a pointer to an int, and initialize a with the address of b.”
This logic also helps understand declarations of multiple items.
Consider int a, *b, c[3];. This says “a, *b, and c[i] are each an int.” From this, we conclude type type of a is an int, the type of b is a pointer to an int, and the type of c is an array of int. (The “picture” logic is stretched by arrays, because the 3 in c[3] tells us the size of the array instead of showing an example use of c. So the analogy is not perfect.)
(This is also why writing int *a fits the C and C++ grammars more closely than int* a does; the formal grammars bind * with a before binding with int.)
1 Kernighan and Ritchie tell us this in The C Programming Language, 1978, page 90:
The declaration of the pointer
pxis new.
int *px;is intended as a mnemonic; it says the combination
*pxis anint, that is, ifpxoccurs in the context*px, it is equivalent to a variable of the typeint. In effect, the syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With