1

One thing that I always found counter-intuitive in the C programming language is how, when defining pointer variables, we don't really declare the type beforehand, but have to put the * before each variable to make it a pointer like this:

int *a, *b;

Instead of doing

int* a, b; // Results in b being an int, not an int*

This can be counterintuitive especially for beginners, when they see a declaration like

int* a;

they might think that just like when you simply do int a; you're declaring the type (int*) and then the variable name. That's why we tend to put the * after the space, so that one realizes it has to be put before each variable name, but still why not defining it so that it would work the way all other types do? Especially considering that the type is referred to as int*. Was is out of consistency with arrays, where they wanted to leave the [] after each variable name so that they could give a different size to each?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
memememe
  • 663
  • 6
  • 21
  • 1
    It is the way it is. Changing the syntax of the language (in the way you suggest) would be bad for all the experienced programmers and existing code. – Fiddling Bits Jul 01 '20 at 20:30
  • I'm not suggesting to change it, just wondering why they made such a choice in the first place. – memememe Jul 01 '20 at 20:31
  • I guess you'd have to ask Dennis Ritchie. – Fiddling Bits Jul 01 '20 at 20:32
  • If you want to avoid all ambiguity, define each variable with it's own semicolon. – Fiddling Bits Jul 01 '20 at 20:34
  • Again, I just wanted to understand *why* it was made this way. And yeah I found an article by Ritchie himself mentioning that it's defined this way to use declarations as similar as possible to usage (so it's like, `*a` has value `int` in `int *a;` https://www.bell-labs.com/usr/dmr/www/chist.html – memememe Jul 01 '20 at 20:39
  • 1
    It's the exact same reason `int a[10], b;` only declares `a` as an array of `int`. C declarations consist of two main parts - a sequence of *declaration specifiers* (type specifiers, type qualifiers, storage class specifiers) followed by a sequence of *declarators*. Pointer-ness, array-ness, and function-ness are specified as part of the *declarator*. Given the declaration `static const unsigned long a[10], *b, c();`, the declaration specifiers are `static const unsigned long`, and the declarators are `a[10]`, `*b`, and `c()`. – John Bode Jul 01 '20 at 20:49
  • 2
    The style of grouping the `*` with the type, as int `int* a` instead of `int *a`, is a modern affectation. I never observed it that way before C++ came along. As you note, it is counter-intuitive, and it misrepresents the grammatical structure of the language as it is formally defined, so it is indeed misleading. Go ahead and group `*` with the identifier. – Eric Postpischil Jul 01 '20 at 21:34
  • It's so that usage mirrors declaration, as explained in the other answer... – CoffeeTableEspresso Jul 01 '20 at 23:02

0 Answers0