4 ms·
There's a perspective that doesn't seem to be noted yet, which is that int *f is declaring that *f will be an int. This perspective addresses why the
by dllthomas 7y ago
There's a perspective that doesn't seem to be noted yet, which is that
int *f
is declaring that
*f
will be an int.
This perspective addresses why the star belongs with the name, why it's star instead of ampersand, why you need to repeat the star for multiple variables, why the brackets goes after the name for arrays, and it will sort of get you where you need to go with function pointers (although there's an automatic promotion which means things will work at the call site that won't work for the type).
This isn't to say alternative constructions mightn't be a better choice in a new language, but it's much more parsimonious when considering C than memorizing a bunch of special cases.
- kevin_thibedeau 7y agoThen someone comes along and typedefs a pointer to hide the "scary" double pointer syntax.
- dllthomas 7y agoI don't think that actually breaks things. A typedef isn't purely syntactic substitution like a macro. If we have typedef float *floatp; then the following compiles: float x, y; floatp xp = &x, yp = &y; while the following does not: float x, y; float* xp = &x, yp = &y; You have defined a new type (which happens to be equivalent to an old type - note that we don't get nominal type-checking from typedefs); the above logic still holds.