3 ms·
Pointer variables in C are declared in an odd way, as `int (*a)` rather than `(int*) a`. I suspect this is also due to B's legacy. In B everything was an integ
by pornel 5y ago
Pointer variables in C are declared in an odd way, as `int (*a)` rather than `(int*) a`. I suspect this is also due to B's legacy.
In B everything was an integer, and there was no separate pointer type. It had only pointer operators that could be applied to any integer.
- veltas 5y agoDeclarators (the name along with pointer, array, etc annotations) in C are written parallel to how they are used, I have always assumed this was to make defining/explaining the language simpler (even though it often defies new learner's expectations), and maybe to reduce the amount of required logic in the compiler. I think you're right that B is related to this, the reason the brackets were on the right of the name is because no type was mentioned in B (as there was only one type). If types had been named in declarations they probably would have preferred to do what Java does, i.e. `Type[] name`. The other more archaic reason the brackets are on the right is because in the oldest B compilers the brackets were not there at all, instead when declaring 'local' (auto) vectors you just write the length of the vector after its name, like `auto name 8;` would declare a vector length 8 called 'name'. Once again this syntax makes sense without the type. In B, declarators had not really matured yet, in some situations you could declare a vector with brackets similar to C arrays, but only of one dimension (which I think is why they were called vectors and not arrays).
- int_19h 5y agoOne thing that I've noticed when digging into historical programming languages of that time period, is that many of them didn't really treat arrays as a type, but rather as some kind of variable modifier - and then putting the brackets after the variable name makes perfect sense. Notably, Algol-60, the granddaddy of all the C-like languages, already had this syntax: begin integer x[1:42]; ... end