Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a logic behind the pointer operator conventions in C/C++? [duplicate]

Tags:

c++

c

* 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

  • Why is the dereference operator (*) also used to declare a pointer? (6 answers)
  • Why use * notation to both define a pointer and dereference one? (1 answer)
  • Why is the dereference operator used to declare pointers? (4 answers)
  • Is the asterisk in the pointer declaration an operator? (3 answers)

My question asks about

  1. the intended logic behind the two symbols * and &
  2. how the * and & symbols can be thought / spoken in words

the 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.

like image 574
doej1367 Avatar asked Dec 12 '25 11:12

doej1367


1 Answers

The logic is this:

  • Unary * always indicates dereferencing of a pointer.
  • When an expression is evaluated, the unary * causes the pointer to be dereferenced.
  • A declaration provides a “picture” of how an identifier will be used in expressions.1 So unary * 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:

  • In a declaration, the = 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.
  • In an expression, the = 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 = &b the 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.)

Footnote

1 Kernighan and Ritchie tell us this in The C Programming Language, 1978, page 90:

The declaration of the pointer px is new.

int *px;

is intended as a mnemonic; it says the combination *px is an int, that is, if px occurs in the context *px, it is equivalent to a variable of the type int. In effect, the syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear.

like image 72
Eric Postpischil Avatar answered Dec 15 '25 00:12

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!