2

Don't get me wrong, I love C/C++ and pointers. I know how to use them, but I have a bone to pick with the "standard" syntax for pointer declaration.

This is not meant to be opinion-based, I am not asking which is better. I am asking why each one is used, and how the system interprets each one.

Also, while I am asking from the view of pointers, this can equally be applied to references.

1. int *p; over int* p;

I know they are the same thing, but the majority of code I've seen uses int *p;. This is tutorials, documentation, and quite a lot of other stackoverflow users. What is the preference for attaching the * to the variable name? While many places use that syntax, I have seen int* p; used for the documentation of function parameters, and compiler messages, so clearly the type is noted as int* to the program. However, this doesn't agree with points 2. and 3.

To me it is clear that int* p; declares a variable p of type pointer-to-int. However, int *p; feels almost like a dereference of some unknown p being declared as an integer. Obviously that is nonsensical so there's no problem reading it as a pointer type, it's just unintuitive.

While I say the dereference is nonsense, it could still be ambiguous to new users in cases like:
int *p = 0x00123456;, which is the declaration of a pointer-to-int vs.
*p = 5;, which is the assignment of an int.

This first part may seem to be a duplicate of other questions, but I am not asking the difference, I understand that int* p; and int *p; are identical.

2. int* p, i; declares a pointer-to-int and an int, but int *p, *pi; declares 2 pointers-to-ints

The second one definitely looks like some nonsensical dereference with variables of type int, whereas the first shows the type to be int*. And why does it even allow int* p, i; as a declaration of different types in one line?

3. Const specifier (and other similar things)

A const int* declares a pointer-to-(const int)
Therefore int* isn't truly being treated as the datatype. If it was, this would be treated as a const (pointer-to-int).

I also don't like how const applies to the thing on it's left, but that's another story ツ

I hope I've explained my concerns well enough, if not please ask me for clarification.
In the end, I understand that this is kinda just preference and style and with a little practice it's easy to read any of this, but I would still love an explanation for the different styles.

Nathan29006781
  • 173
  • 1
  • 9
  • 1
    Related: https://stackoverflow.com/questions/5590150/difference-between-int-p-and-int-p-declaration – SuperStormer Jan 17 '22 at 03:56
  • 2
    @Passerby You should make that an answer and add a little additional analysis for the OP’s specific questions. Especially how `const` works with pointer declarations. And, OP, just because one way makes more sense to you doesn’t mean another makes better sense to others. The reality is that the syntax lends itself to both ways of thinking being “correct”. It’s really too late to quibble now; accept the reality of what it is. – Dúthomhas Jan 17 '22 at 03:57
  • Thanks @SuperStormer I really liked [link](https://stackoverflow.com/a/5590374/14815117) this answer as it makes the same points as me. One thing it reminded me of is my issue with pointer declaration and dereferencing. The same goes for reference declaration and address-of. – Nathan29006781 Jan 17 '22 at 04:01
  • 3
    @Passerby It is common courtesy here on the interwebs to offer others the right to post an answer based on their comments, rather than just post another’s comment as one’s own answer. Sorry for being polite. – Dúthomhas Jan 17 '22 at 04:01
  • 1
    For future reference: This is a **question and answer** site. Note that **question** is singular, not plural. The general rule is that you are allowed to ask one question per post. Numbered lists of questions are never acceptable. You should spend some more time reading the [help] pages to better your understanding of how this site works. – Ken White Jan 17 '22 at 04:14

0 Answers0