5 ms·
C uses `declaration follows use` for its declaration syntax. This means that instead of `char* p` we write `char *p`. https://www.quora.com/Why-doesnt-C-use-bet
by isomorphic- 4y ago
C uses `declaration follows use` for its declaration syntax. This means that instead of `char* p` we write `char *p`. https://www.quora.com/Why-doesnt-C-use-better-notation-for-pointers/answer/Brian-Bi https://www.quora.com/Why-doesnt-C-use-better-notation-for-p...
Also, "decay" is a terrible way to describe the conversion of arrays to pointers-to-their-first-element. "Decaying" implies permanence and that the array is changing/decaying. This isn't the case. The array doesn't permanently change into a pointer. It is the expression that is converted rather than the array itself.
This article is terrible.
- frutiger 4y agoI can’t speak for the rest of the article but for better of for worse “decay” is the standard word used for what happens to arrays as they are passed around as pointers. See for instance https://en.cppreference.com/w/cpp/types/decay https://en.cppreference.com/w/cpp/types/decay.
- skribanto 4y ago`char *p` is identical to `char* p`??
- peterfirefly 4y agoYes, but "char* p,q;" is "char *p,q;" -- q is a char, only p is a pointer.
- jacquesm 4y agoYes, so don't do that. Do: char * p; char q;
- WalterBright 4y agoIn D we write it "char* p, q;" because both p and q are pointers.
- quietbritishjim 4y agoIf you've gone to the trouble of changing the meaning, why not go the whole way and make a more decent syntax? Ideally I'd argue type second, but at least include a separator in there: // Completely clear that both are pointers var p, q: char* I actually think that using the existing syntax for something different, even if it's "fixing" the meaning, is worse than just using the old behaviour.
- WalterBright 4y ago> Completely clear that both are pointers Yes, it is, and "char* p, q;" is also completely clear! (And much more concise, too.) > I actually think that D's use of an existing syntax for something different, even if it's "fixing" the meaning, is worse than just using the old behaviour. At first blush it does seem like a problem. But my experience in translating many, many tens of thousands of lines of code from C to D is making a mistake with that always results in an obvious semantic error that is trivial to fix.
- renox 4y agoIt's the correct word because you loose information when this happens: the size of the array is 'lost' (for 'fixed' arrays, VLA don't have sizes known by the compiler).
- uecker 4y agoWhile the size of the VLA is not (usually) known at compile time, it is part of its type and known at run-time (it is a dependent type). And if you use a pointer to the VLA (and not a decayed pointer), you can recover the size or benefit from run-time bounds checking. int n; char buf[n]; char (*p)[n] = &buf; // non decayed pointer to array sizeof(*p);