Aspie96 on Nostr: In C, sometimes people discuss whether the * should go on the right or the left of ...
In C, sometimes people discuss whether the * should go on the right or the left of the space when declaring a pointer.
I'm of the opinion it should absolutely go on the right, as that is consistent with all other declarations in C. In C, declaration mimics use. It's true you are declaring a variable of int* type, but the declaration should be based on how that is converted to int.
So:
int *a;
Good.
This is in contrast with, for example, D, where the type, simple or not, is on the left of the variable name. Declarations do *not* mimic use. So in D I write "int* a", although both C and D accept both syntaxes.
Now, C++ also has pointers, and of course I use the same syntax for declaring a pointer as I do in C.
C++ also has references, which are very similar, nearly the exact same, and possibly the same at the low level, but not actually the same concept in the language.
The fucking ampersand is used to declare a reference. This does not mimic use.
You cannot convert an int& variable back to int by using the & operator. The use of the ampersand is inconsistent with the rest of the style of the language.
So, if I use the syntax above for C/C++ pointers, with the * on the right side of the space, which should I use for references?
int& a;
or
int &a;
#programming #coding #c #cpp
Published at
2026-08-02 03:27:25 UTCEvent JSON
{
"id": "71b46b8f63a4b45a2780c83fb86d6e9bd7a3b407a1073378914834c558138098",
"pubkey": "8ee4290c98b20b2999138b322f466ea20c75608f568ab53cf11492bf9b2e849c",
"created_at": 1785641245,
"kind": 1,
"tags": [
[
"t",
"programming"
],
[
"t",
"coding"
],
[
"t",
"c"
],
[
"t",
"cpp"
]
],
"content": "In C, sometimes people discuss whether the * should go on the right or the left of the space when declaring a pointer.\nI'm of the opinion it should absolutely go on the right, as that is consistent with all other declarations in C. In C, declaration mimics use. It's true you are declaring a variable of int* type, but the declaration should be based on how that is converted to int.\nSo:\n\nint *a;\n\nGood.\nThis is in contrast with, for example, D, where the type, simple or not, is on the left of the variable name. Declarations do *not* mimic use. So in D I write \"int* a\", although both C and D accept both syntaxes.\n\nNow, C++ also has pointers, and of course I use the same syntax for declaring a pointer as I do in C.\nC++ also has references, which are very similar, nearly the exact same, and possibly the same at the low level, but not actually the same concept in the language.\n\nThe fucking ampersand is used to declare a reference. This does not mimic use.\nYou cannot convert an int\u0026 variable back to int by using the \u0026 operator. The use of the ampersand is inconsistent with the rest of the style of the language.\n\nSo, if I use the syntax above for C/C++ pointers, with the * on the right side of the space, which should I use for references?\n\nint\u0026 a;\nor\nint \u0026a;\n\n#programming #coding #c #cpp",
"sig": "b8589be74c8f1bbd5fcec2e808847860f6cb356f950f9348ffa08cd1659ae58d09bb8ae42a74a84bedcdd339ed5dca304e37fb548a929121ca999cf0edd6247b"
}